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

Commit b6d5439

Browse files
Daniel TabuencaIgorMinar
Daniel Tabuenca
authored andcommitted
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 Closes #5282
1 parent 93901bd commit b6d5439

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ng/directive/input.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
10971097
* It will update the $viewValue, then pass this value through each of the functions in `$parsers`,
10981098
* which includes any validators. The value that comes out of this `$parsers` pipeline, be applied to
10991099
* `$modelValue` and the **expression** specified in the `ng-model` attribute.
1100-
*
1100+
*
11011101
* Lastly, all the registered change listeners, in the `$viewChangeListeners` list, are called.
11021102
*
11031103
* Note that calling this function does not trigger a `$digest`.
@@ -1154,6 +1154,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
11541154
ctrl.$render();
11551155
}
11561156
}
1157+
1158+
return value;
11571159
});
11581160
}];
11591161

test/ng/directive/inputSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,29 @@ describe('ngModel', function() {
383383
dealoc(element);
384384
});
385385
});
386+
387+
it('should keep previously defined watches consistent when changes in validity are made',
388+
inject(function($compile, $rootScope) {
389+
390+
var isFormValid;
391+
$rootScope.$watch('myForm.$valid', function(value) { isFormValid = value; });
392+
393+
var element = $compile('<form name="myForm">' +
394+
'<input name="myControl" ng-model="value" required >' +
395+
'</form>')($rootScope);
396+
397+
$rootScope.$apply();
398+
expect(isFormValid).toBe(false);
399+
expect($rootScope.myForm.$valid).toBe(false);
400+
401+
$rootScope.value='value';
402+
$rootScope.$apply();
403+
expect(isFormValid).toBe(true);
404+
expect($rootScope.myForm.$valid).toBe(true);
405+
406+
dealoc(element);
407+
}));
408+
386409
});
387410

388411

0 commit comments

Comments
 (0)