Skip to content

Commit ab1f1c8

Browse files
committed
fix(ngModel): sync ngModel state with scope state
In cases when we reuse elements in a repeater but associate them with a new scope (see angular#933 - repeating over array of primitives) it's possible for the internal ngModel state and the scope state to get out of sync. This change ensure that the two are always sync-ed up even in cases where we reassociate an element with a different (but similar) scope. In the case of repeating over array of primitives it's still possible to run into issue if we iterate over primitives and use form controls or similar widgets without ngModel - oh well, we'd likely need a special repeater for primitives to deal with this properly, even then there might be cornercases. Closes angular#933
1 parent f24a50a commit ab1f1c8

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/ng/directive/input.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -1010,22 +1010,25 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
10101010

10111011
// model -> value
10121012
var ctrl = this;
1013-
$scope.$watch(ngModelGet, function ngModelWatchAction(value) {
10141013

1015-
// ignore change from view
1016-
if (ctrl.$modelValue === value) return;
1014+
$scope.$watch(function ngModelWatch() {
1015+
var value = ngModelGet($scope);
10171016

1018-
var formatters = ctrl.$formatters,
1019-
idx = formatters.length;
1017+
// if scope model value and ngModel value are out of sync
1018+
if (ctrl.$modelValue !== value) {
10201019

1021-
ctrl.$modelValue = value;
1022-
while(idx--) {
1023-
value = formatters[idx](value);
1024-
}
1020+
var formatters = ctrl.$formatters,
1021+
idx = formatters.length;
10251022

1026-
if (ctrl.$viewValue !== value) {
1027-
ctrl.$viewValue = value;
1028-
ctrl.$render();
1023+
ctrl.$modelValue = value;
1024+
while(idx--) {
1025+
value = formatters[idx](value);
1026+
}
1027+
1028+
if (ctrl.$viewValue !== value) {
1029+
ctrl.$viewValue = value;
1030+
ctrl.$render();
1031+
}
10291032
}
10301033
});
10311034
}];

0 commit comments

Comments
 (0)