refactor(ngModel): move model -> view update from watchFn to watchAction

This commit is contained in:
Martin Staffa
2015-09-23 13:06:27 +02:00
parent 240d5896ec
commit 862c9d8bb2
+10 -13
View File
@@ -731,6 +731,11 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
$exceptionHandler(e);
}
});
if (ctrl.$viewChangeListeners.length > 0 ||
ctrl.$options && ctrl.$options.getterSetter === true
) {
modelToViewAction(ngModelGet($scope));
}
};
/**
@@ -820,19 +825,13 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
}
};
// model -> value
// Note: we cannot use a normal scope.$watch as we want to detect the following:
// 1. scope value is 'a'
// 2. user enters 'b'
// 3. ng-change kicks in and reverts scope value to 'a'
// -> scope value did not change since the last digest as
// ng-change executes in apply phase
// 4. view should be changed back to 'a'
// model -> view
$scope.$watch(function ngModelWatch() {
var modelValue = ngModelGet($scope);
return ngModelGet($scope);
}, ngModelWatchAction, ctrl.$options && ctrl.$options.deepWatch);
function ngModelWatchAction(modelValue) {
// if scope model value and ngModel value are out of sync
// TODO(perf): why not move this to the action fn?
if (modelValue !== ctrl.$modelValue &&
// checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator
(ctrl.$modelValue === ctrl.$modelValue || modelValue === modelValue)
@@ -855,9 +854,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
ctrl.$$runValidators(modelValue, viewValue, noop);
}
}
return modelValue;
});
}
}];