| 29554 | }; |
| 29555 | |
| 29556 | function setupModelWatcher(ctrl) { |
| 29557 | // model -> value |
| 29558 | // Note: we cannot use a normal scope.$watch as we want to detect the following: |
| 29559 | // 1. scope value is 'a' |
| 29560 | // 2. user enters 'b' |
| 29561 | // 3. ng-change kicks in and reverts scope value to 'a' |
| 29562 | // -> scope value did not change since the last digest as |
| 29563 | // ng-change executes in apply phase |
| 29564 | // 4. view should be changed back to 'a' |
| 29565 | ctrl.$$scope.$watch(function ngModelWatch(scope) { |
| 29566 | var modelValue = ctrl.$$ngModelGet(scope); |
| 29567 | |
| 29568 | // if scope model value and ngModel value are out of sync |
| 29569 | // This cannot be moved to the action function, because it would not catch the |
| 29570 | // case where the model is changed in the ngChange function or the model setter |
| 29571 | if (modelValue !== ctrl.$modelValue && |
| 29572 | // checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator |
| 29573 | // eslint-disable-next-line no-self-compare |
| 29574 | (ctrl.$modelValue === ctrl.$modelValue || modelValue === modelValue) |
| 29575 | ) { |
| 29576 | ctrl.$$setModelValue(modelValue); |
| 29577 | } |
| 29578 | |
| 29579 | return modelValue; |
| 29580 | }); |
| 29581 | } |
| 29582 | |
| 29583 | /** |
| 29584 | * @ngdoc method |