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

Commit 6a83149

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 #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 #933
1 parent 6e2c38f commit 6a83149

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
@@ -1011,22 +1011,25 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
10111011

10121012
// model -> value
10131013
var ctrl = this;
1014-
$scope.$watch(ngModelGet, function ngModelWatchAction(value) {
10151014

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

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

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

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

0 commit comments

Comments
 (0)