@@ -799,6 +799,36 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
799
799
}
800
800
} ;
801
801
802
+ function formatValue ( modelValue ) {
803
+ var formatters = ctrl . $formatters ,
804
+ idx = formatters . length ;
805
+
806
+ var viewValue = modelValue ;
807
+ while ( idx -- ) {
808
+ viewValue = formatters [ idx ] ( viewValue ) ;
809
+ }
810
+
811
+ return viewValue ;
812
+ }
813
+
814
+ /**
815
+ * @ngdoc method
816
+ * @name ngModel.NgModelController#$setModelValue
817
+ *
818
+ */
819
+ this . $setModelValue = function ( modelValue ) {
820
+ ctrl . $modelValue = ctrl . $$rawModelValue = modelValue ;
821
+
822
+ var viewValue = formatValue ( this . $modelValue ) ;
823
+
824
+ if ( this . $viewValue !== viewValue ) {
825
+ this . $viewValue = ctrl . $$lastCommittedViewValue = viewValue ;
826
+ this . $render ( ) ;
827
+
828
+ ctrl . $$runValidators ( undefined , modelValue , ctrl . $viewValue , noop ) ;
829
+ }
830
+ } ;
831
+
802
832
// model -> value
803
833
// Note: we cannot use a normal scope.$watch as we want to detect the following:
804
834
// 1. scope value is 'a'
@@ -813,21 +843,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
813
843
// if scope model value and ngModel value are out of sync
814
844
// TODO(perf): why not move this to the action fn?
815
845
if ( modelValue !== ctrl . $modelValue ) {
816
- ctrl . $modelValue = ctrl . $$rawModelValue = modelValue ;
817
-
818
- var formatters = ctrl . $formatters ,
819
- idx = formatters . length ;
820
-
821
- var viewValue = modelValue ;
822
- while ( idx -- ) {
823
- viewValue = formatters [ idx ] ( viewValue ) ;
824
- }
825
- if ( ctrl . $viewValue !== viewValue ) {
826
- ctrl . $viewValue = ctrl . $$lastCommittedViewValue = viewValue ;
827
- ctrl . $render ( ) ;
828
-
829
- ctrl . $$runValidators ( undefined , modelValue , viewValue , noop ) ;
830
- }
846
+ ctrl . $setModelValue ( modelValue ) ;
831
847
}
832
848
833
849
return modelValue ;
0 commit comments