| 30090 | }; |
| 30091 | |
| 30092 | function setupModelWatcher(ctrl) { |
| 30093 | // model -> value |
| 30094 | // Note: we cannot use a normal scope.$watch as we want to detect the following: |
| 30095 | // 1. scope value is 'a' |
| 30096 | // 2. user enters 'b' |
| 30097 | // 3. ng-change kicks in and reverts scope value to 'a' |
| 30098 | // -> scope value did not change since the last digest as |
| 30099 | // ng-change executes in apply phase |
| 30100 | // 4. view should be changed back to 'a' |
| 30101 | ctrl.$$scope.$watch(function ngModelWatch(scope) { |
| 30102 | var modelValue = ctrl.$$ngModelGet(scope); |
| 30103 | |
| 30104 | // if scope model value and ngModel value are out of sync |
| 30105 | // This cannot be moved to the action function, because it would not catch the |
| 30106 | // case where the model is changed in the ngChange function or the model setter |
| 30107 | if (modelValue !== ctrl.$modelValue && |
| 30108 | // checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator |
| 30109 | // eslint-disable-next-line no-self-compare |
| 30110 | (ctrl.$modelValue === ctrl.$modelValue || modelValue === modelValue) |
| 30111 | ) { |
| 30112 | ctrl.$$setModelValue(modelValue); |
| 30113 | } |
| 30114 | |
| 30115 | return modelValue; |
| 30116 | }); |
| 30117 | } |
| 30118 | |
| 30119 | /** |
| 30120 | * @ngdoc method |