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

Commit e6d9bea

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 c8e9105 commit e6d9bea

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

10431043
// model -> value
10441044
var ctrl = this;
1045-
$scope.$watch(ngModelGet, function ngModelWatchAction(value) {
10461045

1047-
// ignore change from view
1048-
if (ctrl.$modelValue === value) return;
1046+
$scope.$watch(function ngModelWatch() {
1047+
var value = ngModelGet($scope);
10491048

1050-
var formatters = ctrl.$formatters,
1051-
idx = formatters.length;
1049+
// if scope model value and ngModel value are out of sync
1050+
if (ctrl.$modelValue !== value) {
10521051

1053-
ctrl.$modelValue = value;
1054-
while(idx--) {
1055-
value = formatters[idx](value);
1056-
}
1052+
var formatters = ctrl.$formatters,
1053+
idx = formatters.length;
10571054

1058-
if (ctrl.$viewValue !== value) {
1059-
ctrl.$viewValue = value;
1060-
ctrl.$render();
1055+
ctrl.$modelValue = value;
1056+
while(idx--) {
1057+
value = formatters[idx](value);
1058+
}
1059+
1060+
if (ctrl.$viewValue !== value) {
1061+
ctrl.$viewValue = value;
1062+
ctrl.$render();
1063+
}
10611064
}
10621065
});
10631066
}];

0 commit comments

Comments
 (0)