Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit facd904

Browse files
shahatapetebacondarwin
authored andcommitted
fix(ngModel): do not dirty the input on $commitViewValue if nothing was changed
Calling `$commitViewValue` was was dirtying the input, even if no update to the view value was made. For example, `updateOn` triggers and form submit may call `$commitViewValue` even if the the view value had not changed. Closes #7457 Closes #7495
1 parent 96fa705 commit facd904

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/ng/directive/input.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1755,8 +1755,11 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
17551755
*/
17561756
this.$commitViewValue = function() {
17571757
var value = ctrl.$viewValue;
1758-
ctrl.$$lastCommittedViewValue = value;
17591758
$timeout.cancel(pendingDebounce);
1759+
if (ctrl.$$lastCommittedViewValue === value) {
1760+
return;
1761+
}
1762+
ctrl.$$lastCommittedViewValue = value;
17601763

17611764
// change to dirty
17621765
if (ctrl.$pristine) {

test/ng/directive/inputSpec.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('NgModelController', function() {
170170

171171
// invalid
172172
ctrl.$parsers.push(function() {return undefined;});
173-
ctrl.$setViewValue('val');
173+
ctrl.$setViewValue('val2');
174174
expect(spy).toHaveBeenCalledOnce();
175175
});
176176

@@ -643,6 +643,16 @@ describe('input', function() {
643643
expect(scope.name).toEqual('a');
644644
});
645645

646+
it('should not dirty the input if nothing was changed before updateOn trigger', function() {
647+
compileInput(
648+
'<input type="text" ng-model="name" name="alias" '+
649+
'ng-model-options="{ updateOn: \'blur\' }"'+
650+
'/>');
651+
652+
browserTrigger(inputElm, 'blur');
653+
expect(scope.form.alias.$pristine).toBeTruthy();
654+
});
655+
646656
it('should allow overriding the model update trigger event on text areas', function() {
647657
compileInput(
648658
'<textarea ng-model="name" name="alias" '+

0 commit comments

Comments
 (0)