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

fix(select) 5449-update-model-properties-when-directly-changed #6645

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,14 +1712,13 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
var ctrl = this;

$scope.$watch(function ngModelWatch() {
var value = ngModelGet($scope);
var value = ngModelGet($scope),
formatters = ctrl.$formatters,
idx = formatters.length;

// if scope model value and ngModel value are out of sync
if (ctrl.$modelValue !== value) {

var formatters = ctrl.$formatters,
idx = formatters.length;

ctrl.$modelValue = value;
while(idx--) {
value = formatters[idx](value);
Expand All @@ -1729,8 +1728,13 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
ctrl.$viewValue = value;
ctrl.$render();
}
}
} else if ((value) && (value instanceof Array) && (value.length === 0)) {
// case where model is modifed directly, this helps to validate the change

while (idx--) {
value = formatters[idx](value);
}
}
return value;
});
}];
Expand Down
10 changes: 10 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,5 +1331,15 @@ describe('select', function() {
}).toThrowMinErr('ng','badname', 'hasOwnProperty is not a valid "option value" name');
});

it('should become invalid if model length is set to zero in scope', function() {
scope.myselect = [];
compile('<select name="selection" ng-model="myselect" required multiple>' +
'<option selected>A</option><option>B</option></select>');
scope.$apply (function() {
scope.myselect.length = 0;
});
expect(element).toBeInvalid();
});

});
});