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

Commit f94d551

Browse files
committed
fix(ngModel): render immediately also with async validators
1 parent 9ad7d74 commit f94d551

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/ng/directive/input.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -2152,12 +2152,11 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
21522152
while(idx--) {
21532153
viewValue = formatters[idx](viewValue);
21542154
}
2155-
var lastViewValue = ctrl.$viewValue;
2156-
if (lastViewValue !== viewValue) {
2157-
ctrl.$$runValidators(undefined, modelValue, viewValue, function() {
2158-
ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;
2159-
ctrl.$render();
2160-
});
2155+
if (ctrl.$viewValue !== viewValue) {
2156+
ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;
2157+
ctrl.$render();
2158+
2159+
ctrl.$$runValidators(undefined, modelValue, viewValue, noop);
21612160
}
21622161
}
21632162

test/ng/directive/inputSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,31 @@ describe('NgModelController', function() {
407407
scope.$apply('value = 5');
408408
expect(ctrl.$render).toHaveBeenCalledOnce();
409409
});
410+
411+
it('should render immediately even if there are async validators', inject(function($q) {
412+
spyOn(ctrl, '$render');
413+
ctrl.$asyncValidators.someValidator = function() {
414+
return $q.defer().promise;
415+
};
416+
417+
scope.$apply('value = 5');
418+
expect(ctrl.$viewValue).toBe(5);
419+
expect(ctrl.$render).toHaveBeenCalledOnce();
420+
}));
421+
422+
it('should not rerender nor validate in case view value is not changed', function() {
423+
ctrl.$formatters.push(function(value) {
424+
return 'nochange';
425+
});
426+
427+
spyOn(ctrl, '$render');
428+
ctrl.$validators.spyValidator = jasmine.createSpy('spyValidator');
429+
scope.$apply('value = "first"');
430+
scope.$apply('value = "second"');
431+
expect(ctrl.$validators.spyValidator).toHaveBeenCalledOnce();
432+
expect(ctrl.$render).toHaveBeenCalledOnce();
433+
});
434+
410435
});
411436

412437
describe('validations pipeline', function() {

0 commit comments

Comments
 (0)