Skip to content

Commit

Permalink
fix(ngModel): update model value with async validators correctly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tbosch authored and Michael Gallagher committed Sep 10, 2014
1 parent 86ab02f commit dd8eb70
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,10 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
break;
}
}
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
// ctrl.$modelValue has not been touched yet...
ctrl.$modelValue = ngModelGet();
}
var prevModelValue = ctrl.$modelValue;
ctrl.$$runValidators(parserValid, modelValue, viewValue, function() {
ctrl.$modelValue = ctrl.$valid ? modelValue : undefined;
Expand Down
34 changes: 34 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,40 @@ describe('NgModelController', function() {
expect(input).not.toHaveClass('ng-valid-parse');
expect(input).toHaveClass('ng-invalid-parse');
});

it('should update the model after all async validators resolve', inject(function($q) {
var defer;
ctrl.$asyncValidators.promiseValidator = function(value) {
defer = $q.defer();
return defer.promise;
};

// set view value on first digest
ctrl.$setViewValue('b');

expect(ctrl.$modelValue).toBeUndefined();
expect(scope.value).toBeUndefined();

defer.resolve();
scope.$digest();

expect(ctrl.$modelValue).toBe('b');
expect(scope.value).toBe('b');

// set view value on further digests
ctrl.$setViewValue('c');

expect(ctrl.$modelValue).toBe('b');
expect(scope.value).toBe('b');

defer.resolve();
scope.$digest();

expect(ctrl.$modelValue).toBe('c');
expect(scope.value).toBe('c');

}));

});


Expand Down

0 comments on commit dd8eb70

Please sign in to comment.