Skip to content

Commit

Permalink
fix(ngModel): do not dirty the input on $commitViewValue if nothing w…
Browse files Browse the repository at this point in the history
…as 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 angular#7457
Closes angular#7495
  • Loading branch information
shahata authored and RichardLitt committed May 24, 2014
1 parent cbfdd09 commit f439c0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1755,8 +1755,11 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
*/
this.$commitViewValue = function() {
var value = ctrl.$viewValue;
ctrl.$$lastCommittedViewValue = value;
$timeout.cancel(pendingDebounce);
if (ctrl.$$lastCommittedViewValue === value) {
return;
}
ctrl.$$lastCommittedViewValue = value;

// change to dirty
if (ctrl.$pristine) {
Expand Down
12 changes: 11 additions & 1 deletion test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('NgModelController', function() {

// invalid
ctrl.$parsers.push(function() {return undefined;});
ctrl.$setViewValue('val');
ctrl.$setViewValue('val2');
expect(spy).toHaveBeenCalledOnce();
});

Expand Down Expand Up @@ -643,6 +643,16 @@ describe('input', function() {
expect(scope.name).toEqual('a');
});

it('should not dirty the input if nothing was changed before updateOn trigger', function() {
compileInput(
'<input type="text" ng-model="name" name="alias" '+
'ng-model-options="{ updateOn: \'blur\' }"'+
'/>');

browserTrigger(inputElm, 'blur');
expect(scope.form.alias.$pristine).toBeTruthy();
});

it('should allow overriding the model update trigger event on text areas', function() {
compileInput(
'<textarea ng-model="name" name="alias" '+
Expand Down

0 comments on commit f439c0a

Please sign in to comment.