From a57f470325d7f49ae111e776e18bd8794b62d9a6 Mon Sep 17 00:00:00 2001 From: Daniel Tabuenca Date: Wed, 4 Dec 2013 16:25:49 -0800 Subject: [PATCH] fix(input): ensure ngModelWatch() triggers second digest pass when appropriate Due to an earlier change, ngModelWatch() no longer returns a value to the caller. This means the digest loop has no way to tell if the watch actually modified anything and so can not schedule another pass. This means any watches that watch form or model controller changes (e.g. watches on form.$valid) that are scheduled prior to an ngModelWatch() will not be able to see any changes made therin. This commit fixes this behavior by returning the latest evaluated ng-model value. Closes #5258 --- src/ng/directive/input.js | 2 ++ test/ng/directive/inputSpec.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 1d8d14eef6e5..18ff06b1fa4e 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1154,6 +1154,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ ctrl.$render(); } } + + return value; }); }]; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 892c1b7f534d..c568e807d112 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -383,6 +383,29 @@ describe('ngModel', function() { dealoc(element); }); }); + + it('should keep previously defined watches consistent when changes in validity are made', + inject(function($compile, $rootScope) { + + var isFormValid; + $rootScope.$watch('myForm.$valid', function(value) { isFormValid = value; }); + + var element = $compile('
' + + '' + + '
')($rootScope); + + $rootScope.$apply(); + expect(isFormValid).toBe(false); + expect($rootScope.myForm.$valid).toBe(false); + + $rootScope.value='value'; + $rootScope.$apply(); + expect(isFormValid).toBe(true); + expect($rootScope.myForm.$valid).toBe(true); + + dealoc(element); + })); + });