diff options
| author | Igor Minar | 2012-11-23 22:40:01 +0100 |
|---|---|---|
| committer | Igor Minar | 2012-11-26 20:36:53 +0100 |
| commit | e6d9bea4f3b2eb28851298d3dc3a30d46062d58a (patch) | |
| tree | 1ee612d8a3ef0aad796fbf2bd8564b65c4270508 /src/ng/directive/input.js | |
| parent | c8e9105fe685e95340336b57cb85faebbc7955cc (diff) | |
| download | angular.js-e6d9bea4f3b2eb28851298d3dc3a30d46062d58a.tar.bz2 | |
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
Diffstat (limited to 'src/ng/directive/input.js')
| -rw-r--r-- | src/ng/directive/input.js | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 695ca88a..aaabd103 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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(); + } } }); }]; |
