| 31169 | }; |
| 31170 | |
| 31171 | function setupModelWatcher(ctrl) { |
| 31172 | // model -> value |
| 31173 | // Note: we cannot use a normal scope.$watch as we want to detect the following: |
| 31174 | // 1. scope value is 'a' |
| 31175 | // 2. user enters 'b' |
| 31176 | // 3. ng-change kicks in and reverts scope value to 'a' |
| 31177 | // -> scope value did not change since the last digest as |
| 31178 | // ng-change executes in apply phase |
| 31179 | // 4. view should be changed back to 'a' |
| 31180 | ctrl.$$scope.$watch(function ngModelWatch(scope) { |
| 31181 | var modelValue = ctrl.$$ngModelGet(scope); |
| 31182 | |
| 31183 | // if scope model value and ngModel value are out of sync |
| 31184 | // This cannot be moved to the action function, because it would not catch the |
| 31185 | // case where the model is changed in the ngChange function or the model setter |
| 31186 | if (modelValue !== ctrl.$modelValue && |
| 31187 | // checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator |
| 31188 | // eslint-disable-next-line no-self-compare |
| 31189 | (ctrl.$modelValue === ctrl.$modelValue || modelValue === modelValue) |
| 31190 | ) { |
| 31191 | ctrl.$$setModelValue(modelValue); |
| 31192 | } |
| 31193 | |
| 31194 | return modelValue; |
| 31195 | }); |
| 31196 | } |
| 31197 | |
| 31198 | /** |
| 31199 | * @ngdoc method |