Skip to content

Commit

Permalink
fix(ngModel): sync ngModel state with scope state
Browse files Browse the repository at this point in the history
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
  • Loading branch information
IgorMinar committed Nov 23, 2012
1 parent f24a50a commit ab1f1c8
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,22 +1010,25 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$

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

// ignore change from view
if (ctrl.$modelValue === value) return;
$scope.$watch(function ngModelWatch() {
var value = ngModelGet($scope);

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

ctrl.$modelValue = value;
while(idx--) {
value = formatters[idx](value);
}
var formatters = ctrl.$formatters,
idx = formatters.length;

if (ctrl.$viewValue !== value) {
ctrl.$viewValue = value;
ctrl.$render();
ctrl.$modelValue = value;
while(idx--) {
value = formatters[idx](value);
}

if (ctrl.$viewValue !== value) {
ctrl.$viewValue = value;
ctrl.$render();
}
}
});
}];
Expand Down

0 comments on commit ab1f1c8

Please sign in to comment.