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

Commit dd2a803

Browse files
Arturo Guzmanpetebacondarwin
Arturo Guzman
authored andcommitted
perf(input): prevent additional $digest when input is already touched
@kevinjamesus86 noticed that the input control would trigger a $digest cycle every time it was blurred, adcc5a0#commitcomment-7129512. After the control is in a $touched state, other $digest cycles are unnecesary. Closes #8450
1 parent 7884c25 commit dd2a803

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/ng/directive/input.js

+2
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,8 @@ var ngModelDirective = function() {
21822182
}
21832183

21842184
element.on('blur', function(ev) {
2185+
if (modelCtrl.$touched) return;
2186+
21852187
scope.$apply(function() {
21862188
modelCtrl.$setTouched();
21872189
});

test/ng/directive/inputSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,23 @@ describe('ngModel', function() {
472472
dealoc(element);
473473
}));
474474

475+
it('should not cause a digest on "blur" event if control is already touched',
476+
inject(function($compile, $rootScope) {
477+
478+
var element = $compile('<form name="myForm">' +
479+
'<input name="myControl" ng-model="value" >' +
480+
'</form>')($rootScope);
481+
var inputElm = element.find('input');
482+
var control = $rootScope.myForm.myControl;
483+
484+
control.$setTouched();
485+
spyOn($rootScope, '$apply');
486+
browserTrigger(inputElm, 'blur');
487+
488+
expect($rootScope.$apply).not.toHaveBeenCalled();
489+
490+
dealoc(element);
491+
}));
475492

476493
it('should register/deregister a nested ngModel with parent form when entering or leaving DOM',
477494
inject(function($compile, $rootScope) {

0 commit comments

Comments
 (0)