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

Commit 64c3b74

Browse files
committed
fix(ngModel): update model value with async validators correctly
If the view value changed in the first digest and there are async validators, the view value was never applied to the model after the validators were resolved. Only important for tests.
1 parent f94d551 commit 64c3b74

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/ng/directive/input.js

+4
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,10 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
20322032
break;
20332033
}
20342034
}
2035+
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
2036+
// ctrl.$modelValue has not been touched yet...
2037+
ctrl.$modelValue = ngModelGet();
2038+
}
20352039
var prevModelValue = ctrl.$modelValue;
20362040
ctrl.$$runValidators(parserValid, modelValue, viewValue, function() {
20372041
ctrl.$modelValue = ctrl.$valid ? modelValue : undefined;

test/ng/directive/inputSpec.js

+34
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,40 @@ describe('NgModelController', function() {
356356
expect(input).not.toHaveClass('ng-valid-parse');
357357
expect(input).toHaveClass('ng-invalid-parse');
358358
});
359+
360+
it('should update the model after all async validators resolve', inject(function($q) {
361+
var defer;
362+
ctrl.$asyncValidators.promiseValidator = function(value) {
363+
defer = $q.defer();
364+
return defer.promise;
365+
};
366+
367+
// set view value on first digest
368+
ctrl.$setViewValue('b');
369+
370+
expect(ctrl.$modelValue).toBeUndefined();
371+
expect(scope.value).toBeUndefined();
372+
373+
defer.resolve();
374+
scope.$digest();
375+
376+
expect(ctrl.$modelValue).toBe('b');
377+
expect(scope.value).toBe('b');
378+
379+
// set view value on further digests
380+
ctrl.$setViewValue('c');
381+
382+
expect(ctrl.$modelValue).toBe('b');
383+
expect(scope.value).toBe('b');
384+
385+
defer.resolve();
386+
scope.$digest();
387+
388+
expect(ctrl.$modelValue).toBe('c');
389+
expect(scope.value).toBe('c');
390+
391+
}));
392+
359393
});
360394

361395

0 commit comments

Comments
 (0)