From bb0ba73ce5644dc475198115fc54a062524228f8 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Sat, 17 May 2014 14:01:35 +0300 Subject: [PATCH] fix(ngModel): do not dirty the input if nothing was changed This caused the input to become dirty every time `$commitViewValue` was called, even if no update to the view value was made. For example, `updateOn` triggers and form submit may call `$commitViewValue` without updating the view value. Closes #7457 --- src/ng/directive/input.js | 5 ++++- test/ng/directive/inputSpec.js | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 0c7664e1d02e..d7676f2eb96c 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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) { diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index e1d7ff2c9e4e..c026f9a455a9 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -170,7 +170,7 @@ describe('NgModelController', function() { // invalid ctrl.$parsers.push(function() {return undefined;}); - ctrl.$setViewValue('val'); + ctrl.$setViewValue('val2'); expect(spy).toHaveBeenCalledOnce(); }); @@ -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( + ''); + + browserTrigger(inputElm, 'blur'); + expect(scope.form.alias.$pristine).toBeTruthy(); + }); + it('should allow overriding the model update trigger event on text areas', function() { compileInput( '