@@ -806,43 +806,67 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
806
806
}
807
807
} ;
808
808
809
- // model -> value
810
- // Note: we cannot use a normal scope.$watch as we want to detect the following:
811
- // 1. scope value is 'a'
812
- // 2. user enters 'b'
813
- // 3. ng-change kicks in and reverts scope value to 'a'
814
- // -> scope value did not change since the last digest as
815
- // ng-change executes in apply phase
816
- // 4. view should be changed back to 'a'
817
- $scope . $watch ( function ngModelWatch ( ) {
809
+ this . $$setupModelWatch = function ( ) {
810
+ // model -> value
811
+ // Note: we cannot use a normal scope.$watch as we want to detect the following:
812
+ // 1. scope value is 'a'
813
+ // 2. user enters 'b'
814
+ // 3. ng-change kicks in and reverts scope value to 'a'
815
+ // -> scope value did not change since the last digest as
816
+ // ng-change executes in apply phase
817
+ // 4. view should be changed back to 'a'
818
+
819
+ // options.deepWatch
820
+ // options.collection
821
+
822
+ getModelValueSetter ( ) ;
823
+ $scope . $watch ( ngModelWatch ) ;
824
+ } ;
825
+
826
+ var modelValueSetter = function modelValueSetterDefault ( modelValue ) {
827
+ return modelValue ;
828
+ } ;
829
+
830
+ function getModelValueSetter ( ) {
831
+ if ( ctrl . $options && ctrl . $options . deepWatch ) {
832
+ modelValueSetter = function modelValueSetterCopy ( modelValue ) {
833
+ return copy ( modelValue ) ;
834
+ } ;
835
+ }
836
+ }
837
+
838
+ function ngModelWatch ( ) {
818
839
var modelValue = ngModelGet ( $scope ) ;
819
840
820
841
// if scope model value and ngModel value are out of sync
821
- // TODO(perf): why not move this to the action fn?
822
842
if ( modelValue !== ctrl . $modelValue &&
823
843
// checks for NaN is needed to allow setting the model to NaN when there's an asyncValidator
824
844
( ctrl . $modelValue === ctrl . $modelValue || modelValue === modelValue )
825
845
) {
826
- ctrl . $modelValue = ctrl . $$rawModelValue = modelValue ;
827
- parserValid = undefined ;
846
+ modelToViewAction ( modelValue ) ;
847
+ }
848
+ return modelValue ;
849
+ }
828
850
829
- var formatters = ctrl . $formatters ,
830
- idx = formatters . length ;
851
+ function modelToViewAction ( modelValue ) {
852
+ ctrl . $modelValue = ctrl . $$rawModelValue = modelValueSetter ( modelValue ) ;
853
+ parserValid = undefined ;
831
854
832
- var viewValue = modelValue ;
833
- while ( idx -- ) {
834
- viewValue = formatters [ idx ] ( viewValue ) ;
835
- }
836
- if ( ctrl . $viewValue !== viewValue ) {
837
- ctrl . $viewValue = ctrl . $$lastCommittedViewValue = viewValue ;
838
- ctrl . $render ( ) ;
855
+ var formatters = ctrl . $formatters ,
856
+ idx = formatters . length ;
839
857
840
- ctrl . $$runValidators ( modelValue , viewValue , noop ) ;
841
- }
858
+ var viewValue = modelValue ;
859
+ while ( idx -- ) {
860
+ viewValue = formatters [ idx ] ( viewValue ) ;
842
861
}
862
+ if ( ctrl . $viewValue !== viewValue ) {
863
+ ctrl . $viewValue = ctrl . $$lastCommittedViewValue = viewValue ;
864
+ ctrl . $render ( ) ;
865
+
866
+ ctrl . $$runValidators ( modelValue , viewValue , noop ) ;
867
+ }
868
+ }
843
869
844
- return modelValue ;
845
- } ) ;
846
870
} ] ;
847
871
848
872
@@ -1032,6 +1056,7 @@ var ngModelDirective = ['$rootScope', function($rootScope) {
1032
1056
formCtrl = ctrls [ 1 ] || modelCtrl . $$parentForm ;
1033
1057
1034
1058
modelCtrl . $$setOptions ( ctrls [ 2 ] && ctrls [ 2 ] . $options ) ;
1059
+ modelCtrl . $$setupModelWatch ( ) ;
1035
1060
1036
1061
// notify others, especially parent forms
1037
1062
formCtrl . $addControl ( modelCtrl ) ;
0 commit comments