| 31104 | }; |
| 31105 | |
| 31106 | function setupModelWatcher(ctrl) { |
| 31107 | // model -> value |
| 31108 | // Note: we cannot use a normal scope.$watch as we want to detect the following: |
| 31109 | // 1. scope value is 'a' |
| 31110 | // 2. user enters 'b' |
| 31111 | // 3. ng-change kicks in and reverts scope value to 'a' |
| 31112 | // -> scope value did not change since the last digest as |
| 31113 | // ng-change executes in apply phase |
| 31114 | // 4. view should be changed back to 'a' |
| 31115 | ctrl.$$scope.$watch(function ngModelWatch(scope) { |
| 31116 | var modelValue = ctrl.$$ngModelGet(scope); |
| 31117 | |
| 31118 | // if scope model value and ngModel value are out of sync |
| 31119 | // This cannot be moved to the action function, because it would not catch the |
| 31120 | // case where the model is changed in the ngChange function or the model setter |
| 31121 | if (modelValue !== ctrl.$modelValue && |
| 31122 | // checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator |
| 31123 | // eslint-disable-next-line no-self-compare |
| 31124 | (ctrl.$modelValue === ctrl.$modelValue || modelValue === modelValue) |
| 31125 | ) { |
| 31126 | ctrl.$$setModelValue(modelValue); |
| 31127 | } |
| 31128 | |
| 31129 | return modelValue; |
| 31130 | }); |
| 31131 | } |
| 31132 | |
| 31133 | /** |
| 31134 | * @ngdoc method |