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

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 #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
  • Loading branch information
IgorMinar committed Nov 26, 2012
1 parent c8e9105 commit e6d9bea
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 @@ -1042,22 +1042,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 e6d9bea

Please sign in to comment.