| 30207 | }; |
| 30208 | |
| 30209 | function setupModelWatcher(ctrl) { |
| 30210 | // model -> value |
| 30211 | // Note: we cannot use a normal scope.$watch as we want to detect the following: |
| 30212 | // 1. scope value is 'a' |
| 30213 | // 2. user enters 'b' |
| 30214 | // 3. ng-change kicks in and reverts scope value to 'a' |
| 30215 | // -> scope value did not change since the last digest as |
| 30216 | // ng-change executes in apply phase |
| 30217 | // 4. view should be changed back to 'a' |
| 30218 | ctrl.$$scope.$watch(function ngModelWatch(scope) { |
| 30219 | var modelValue = ctrl.$$ngModelGet(scope); |
| 30220 | |
| 30221 | // if scope model value and ngModel value are out of sync |
| 30222 | // This cannot be moved to the action function, because it would not catch the |
| 30223 | // case where the model is changed in the ngChange function or the model setter |
| 30224 | if (modelValue !== ctrl.$modelValue && |
| 30225 | // checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator |
| 30226 | // eslint-disable-next-line no-self-compare |
| 30227 | (ctrl.$modelValue === ctrl.$modelValue || modelValue === modelValue) |
| 30228 | ) { |
| 30229 | ctrl.$$setModelValue(modelValue); |
| 30230 | } |
| 30231 | |
| 30232 | return modelValue; |
| 30233 | }); |
| 30234 | } |
| 30235 | |
| 30236 | /** |
| 30237 | * @ngdoc method |