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

Commit eab2718

Browse files
alexanderchanNarretz
authored andcommitted
fix(input): call $setTouched in blur asynchronously if necessary
If the model is blurred during an apply it should trigger $setTouched asynchronously. Fixes #8762 Fixes #9808 Closes #10014
1 parent 637d3b4 commit eab2718

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/ng/directive/input.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2497,7 +2497,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
24972497
</file>
24982498
* </example>
24992499
*/
2500-
var ngModelDirective = function() {
2500+
var ngModelDirective = ['$rootScope', function($rootScope) {
25012501
return {
25022502
restrict: 'A',
25032503
require: ['ngModel', '^?form', '^?ngModelOptions'],
@@ -2541,15 +2541,17 @@ var ngModelDirective = function() {
25412541
element.on('blur', function(ev) {
25422542
if (modelCtrl.$touched) return;
25432543

2544-
scope.$apply(function() {
2545-
modelCtrl.$setTouched();
2546-
});
2544+
if ($rootScope.$$phase) {
2545+
scope.$evalAsync(modelCtrl.$setTouched);
2546+
} else {
2547+
scope.$apply(modelCtrl.$setTouched);
2548+
}
25472549
});
25482550
}
25492551
};
25502552
}
25512553
};
2552-
};
2554+
}];
25532555

25542556

25552557
/**

test/ng/directive/inputSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,32 @@ describe('ngModel', function() {
12801280
dealoc(element);
12811281
}));
12821282

1283+
it('should digest asynchronously on "blur" event if a apply is already in progress',
1284+
inject(function($compile, $rootScope) {
1285+
1286+
var element = $compile('<form name="myForm">' +
1287+
'<input name="myControl" ng-model="value" >' +
1288+
'</form>')($rootScope);
1289+
var inputElm = element.find('input');
1290+
var control = $rootScope.myForm.myControl;
1291+
1292+
$rootScope.$apply(function() {
1293+
expect(control.$touched).toBe(false);
1294+
expect(control.$untouched).toBe(true);
1295+
1296+
browserTrigger(inputElm, 'blur');
1297+
1298+
expect(control.$touched).toBe(false);
1299+
expect(control.$untouched).toBe(true);
1300+
});
1301+
1302+
expect(control.$touched).toBe(true);
1303+
expect(control.$untouched).toBe(false);
1304+
1305+
dealoc(element);
1306+
}));
1307+
1308+
12831309
it('should register/deregister a nested ngModel with parent form when entering or leaving DOM',
12841310
inject(function($compile, $rootScope) {
12851311

0 commit comments

Comments
 (0)