@@ -799,6 +799,41 @@ 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 , fromModel ) {
820
+ var previousModelValue = ctrl . $modelValue ;
821
+ ctrl . $modelValue = ctrl . $$rawModelValue = modelValue ;
822
+
823
+ if ( ! fromModel && previousModelValue !== ctrl . $modelValue ) {
824
+ this . $$writeModelToScope ( ) ;
825
+ }
826
+
827
+ var viewValue = formatValue ( this . $modelValue ) ;
828
+
829
+ if ( this . $viewValue !== viewValue ) {
830
+ this . $viewValue = ctrl . $$lastCommittedViewValue = viewValue ;
831
+ this . $render ( ) ;
832
+
833
+ ctrl . $$runValidators ( undefined , modelValue , ctrl . $viewValue , noop ) ;
834
+ }
835
+ } ;
836
+
802
837
// model -> value
803
838
// Note: we cannot use a normal scope.$watch as we want to detect the following:
804
839
// 1. scope value is 'a'
@@ -813,21 +848,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
813
848
// if scope model value and ngModel value are out of sync
814
849
// TODO(perf): why not move this to the action fn?
815
850
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
- }
851
+ ctrl . $setModelValue ( modelValue , true ) ;
831
852
}
832
853
833
854
return modelValue ;
0 commit comments