mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
fix(forms): Propagate change from model even if it's undefined
This commit is contained in:
parent
cce31d4c93
commit
1b9277bf6f
2 changed files with 30 additions and 7 deletions
|
|
@ -847,7 +847,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel',
|
|||
value = formatters[idx](value);
|
||||
}
|
||||
|
||||
if (isDefined(value) && ctrl.viewValue !== value) {
|
||||
if (ctrl.viewValue !== value) {
|
||||
ctrl.viewValue = value;
|
||||
ctrl.render();
|
||||
}
|
||||
|
|
@ -1042,7 +1042,7 @@ var requiredDirective = [function() {
|
|||
if (!ctrl) return;
|
||||
|
||||
var validator = function(value) {
|
||||
if (attr.required && isEmpty(value)) {
|
||||
if (attr.required && (isEmpty(value) || value === false)) {
|
||||
ctrl.setValidity('REQUIRED', false);
|
||||
return null;
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -183,22 +183,32 @@ describe('NgModelController', function() {
|
|||
});
|
||||
|
||||
|
||||
it('should $render only if value changed and is valid', function() {
|
||||
it('should $render only if value changed', function() {
|
||||
spyOn(ctrl, 'render');
|
||||
|
||||
scope.$apply(function() {
|
||||
scope.value= 3;
|
||||
scope.value = 3;
|
||||
});
|
||||
expect(ctrl.render).toHaveBeenCalledOnce();
|
||||
ctrl.render.reset();
|
||||
|
||||
// invalid
|
||||
ctrl.formatters.push(function() {return undefined;});
|
||||
ctrl.formatters.push(function() {return 3;});
|
||||
scope.$apply(function() {
|
||||
scope.value= 5;
|
||||
scope.value = 5;
|
||||
});
|
||||
expect(ctrl.render).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it('should clear the view even if invalid', function() {
|
||||
spyOn(ctrl, 'render');
|
||||
|
||||
ctrl.formatters.push(function() {return undefined;});
|
||||
scope.$apply(function() {
|
||||
scope.value = 5;
|
||||
});
|
||||
expect(ctrl.render).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -729,6 +739,19 @@ describe('input', function() {
|
|||
browserTrigger(inputElm, 'click');
|
||||
expect(scope.name).toEqual('n');
|
||||
});
|
||||
|
||||
|
||||
it('should be required if false', function() {
|
||||
compileInput('<input type="checkbox" ng:model="value" required />');
|
||||
|
||||
browserTrigger(inputElm, 'click');
|
||||
expect(inputElm[0].checked).toBe(true);
|
||||
expect(inputElm).toBeValid();
|
||||
|
||||
browserTrigger(inputElm, 'click');
|
||||
expect(inputElm[0].checked).toBe(false);
|
||||
expect(inputElm).toBeInvalid();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue