mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
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
This commit is contained in:
parent
c8e9105fe6
commit
e6d9bea4f3
1 changed files with 15 additions and 12 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
});
|
||||
}];
|
||||
|
|
|
|||
Loading…
Reference in a new issue