@@ -878,6 +878,27 @@ NgModelController.prototype = {
878
878
*/
879
879
$overrideModelOptions : function ( options ) {
880
880
this . $options = this . $options . createChild ( options ) ;
881
+ } ,
882
+
883
+ $setModelValue : function ( modelValue ) {
884
+ this . $modelValue = this . $$rawModelValue = modelValue ;
885
+ this . $$parserValid = undefined ;
886
+
887
+ var formatters = this . $formatters ,
888
+ idx = formatters . length ;
889
+
890
+ var viewValue = modelValue ;
891
+ while ( idx -- ) {
892
+ viewValue = formatters [ idx ] ( viewValue ) ;
893
+ }
894
+ if ( this . $viewValue !== viewValue ) {
895
+ this . $$updateEmptyClasses ( viewValue ) ;
896
+ this . $viewValue = this . $$lastCommittedViewValue = viewValue ;
897
+ this . $render ( ) ;
898
+
899
+ // It is possible that model and view value have been updated during render
900
+ this . $$runValidators ( this . $modelValue , this . $viewValue , noop ) ;
901
+ }
881
902
}
882
903
} ;
883
904
@@ -894,33 +915,15 @@ function setupModelWatcher(ctrl) {
894
915
var modelValue = ctrl . $$ngModelGet ( scope ) ;
895
916
896
917
// if scope model value and ngModel value are out of sync
897
- // TODO(perf): why not move this to the action fn?
918
+ // This cannot be moved to the action function, because it would not catch the
919
+ // case where the model is changed in the ngChange function or the model setter
898
920
if ( modelValue !== ctrl . $modelValue &&
899
- // checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator
900
- // eslint-disable-next-line no-self-compare
901
- ( ctrl . $modelValue === ctrl . $modelValue || modelValue === modelValue )
921
+ // checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator
922
+ // eslint-disable-next-line no-self-compare
923
+ ( ctrl . $modelValue === ctrl . $modelValue || modelValue === modelValue )
902
924
) {
903
- ctrl . $modelValue = ctrl . $$rawModelValue = modelValue ;
904
- ctrl . $$parserValid = undefined ;
905
-
906
- var formatters = ctrl . $formatters ,
907
- idx = formatters . length ;
908
-
909
- var viewValue = modelValue ;
910
- while ( idx -- ) {
911
- viewValue = formatters [ idx ] ( viewValue ) ;
912
- }
913
- if ( ctrl . $viewValue !== viewValue ) {
914
- ctrl . $$updateEmptyClasses ( viewValue ) ;
915
- ctrl . $viewValue = ctrl . $$lastCommittedViewValue = viewValue ;
916
- ctrl . $render ( ) ;
917
-
918
- // It is possible that model and view value have been updated during render
919
- ctrl . $$runValidators ( ctrl . $modelValue , ctrl . $viewValue , noop ) ;
920
- }
925
+ ctrl . $setModelValue ( modelValue ) ;
921
926
}
922
-
923
- return modelValue ;
924
927
} ) ;
925
928
}
926
929
0 commit comments