Skip to content

Commit fc9171b

Browse files
author
Gonzalo Ruiz de Villa
committed
fix(input): ngList is updated when array model values are changed
fixes angular#1751 When the model referenced the same same array and the array values where changed, the list wasn't updated. Now watchCollection is used to detect changes in NgModelController. isEmpty now detects empty arrays BEHAVIOUR CHANGE There is a change in the behaviour of ngList when typing a list. When “a , b” is typed is automatically changed to “a, b”.
1 parent 5d1e2e2 commit fc9171b

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/ng/directive/input.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,9 @@ var inputType = {
385385

386386

387387
function isEmpty(value) {
388-
return isUndefined(value) || value === '' || value === null || value !== value;
388+
return isUndefined(value) || value === '' || value === null ||
389+
(value.length === 0 && isArrayLike(value)) ||
390+
value !== value;
389391
}
390392

391393

@@ -1082,11 +1084,11 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
10821084
// model -> value
10831085
var ctrl = this;
10841086

1085-
$scope.$watch(function ngModelWatch() {
1087+
$scope.$watchCollection($attr.ngModel, function ngModelWatch(newValue, oldValue) {
10861088
var value = ngModelGet($scope);
10871089

10881090
// if scope model value and ngModel value are out of sync
1089-
if (ctrl.$modelValue !== value) {
1091+
if (!equals(ctrl.$modelValue, oldValue)) {
10901092

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

test/ng/directive/inputSpec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ describe('input', function() {
986986
expect(scope.list).toEqual(['a']);
987987

988988
changeInputValueTo('a , b');
989-
expect(inputElm.val()).toEqual('a , b');
989+
expect(inputElm.val()).toEqual('a, b');
990990
expect(scope.list).toEqual(['a', 'b']);
991991
});
992992

@@ -1019,6 +1019,28 @@ describe('input', function() {
10191019
changeInputValueTo('a,b: c');
10201020
expect(scope.list).toEqual(['a', 'b', 'c']);
10211021
});
1022+
1023+
it("should detect changes in the values of an array", function () {
1024+
var list = ['x', 'y', 'z'];
1025+
compileInput('<input type="text" ng-model="list" ng-list />');
1026+
scope.$apply(function() {
1027+
scope.list = list;
1028+
});
1029+
expect(inputElm.val()).toBe('x, y, z');
1030+
scope.$apply(function() {
1031+
list.unshift('w');
1032+
});
1033+
expect(inputElm.val()).toBe('w, x, y, z');
1034+
});
1035+
1036+
it('should be invalid if empty', function() {
1037+
compileInput('<input name="namesInput" ng-model="list" ng-list required/>');
1038+
changeInputValueTo('a');
1039+
expect(inputElm).toBeValid();
1040+
changeInputValueTo('');
1041+
expect(inputElm).toBeInvalid();
1042+
});
1043+
10221044
});
10231045

10241046
describe('required', function() {

0 commit comments

Comments
 (0)