Skip to content

Commit cb540c7

Browse files
committed
fix:(input) asynchronous ng-model blur
If the model is blurred during an apply it should trigger the $touched asynchronously. Fixes angular#8762
1 parent df3d739 commit cb540c7

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/ng/directive/input.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -2460,9 +2460,14 @@ var ngModelDirective = function() {
24602460
element.on('blur', function(ev) {
24612461
if (modelCtrl.$touched) return;
24622462

2463-
scope.$apply(function() {
2464-
modelCtrl.$setTouched();
2465-
});
2463+
var onBlurSetTouched = function() {
2464+
modelCtrl.$setTouched();
2465+
};
2466+
if (scope.$$phase) {
2467+
scope.$evalAsync(onBlurSetTouched);
2468+
} else {
2469+
scope.$apply(onBlurSetTouched);
2470+
}
24662471
});
24672472
}
24682473
};

test/ng/directive/inputSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,32 @@ describe('ngModel', function() {
11441144
dealoc(element);
11451145
}));
11461146

1147+
it('should digest asynchronously on "blur" event if a apply is already in progress',
1148+
inject(function($compile, $rootScope) {
1149+
1150+
var element = $compile('<form name="myForm">' +
1151+
'<input name="myControl" ng-model="value" >' +
1152+
'</form>')($rootScope);
1153+
var inputElm = element.find('input');
1154+
var control = $rootScope.myForm.myControl;
1155+
1156+
$rootScope.$apply(function() {
1157+
expect(control.$touched).toBe(false);
1158+
expect(control.$untouched).toBe(true);
1159+
1160+
browserTrigger(inputElm, 'blur');
1161+
1162+
expect(control.$touched).toBe(false);
1163+
expect(control.$untouched).toBe(true);
1164+
});
1165+
1166+
expect(control.$touched).toBe(true);
1167+
expect(control.$untouched).toBe(false);
1168+
1169+
dealoc(element);
1170+
}));
1171+
1172+
11471173
it('should register/deregister a nested ngModel with parent form when entering or leaving DOM',
11481174
inject(function($compile, $rootScope) {
11491175

0 commit comments

Comments
 (0)