6
6
DIRTY_CLASS: true,
7
7
UNTOUCHED_CLASS: true,
8
8
TOUCHED_CLASS: true,
9
+ $ModelOptionsProvider: true,
9
10
*/
10
11
11
12
var VALID_CLASS = 'ng-valid' ,
@@ -217,8 +218,8 @@ is set to `true`. The parse error is stored in `ngModel.$error.parse`.
217
218
*
218
219
*
219
220
*/
220
- var NgModelController = [ '$scope' , '$exceptionHandler' , '$attrs' , '$element' , '$parse' , '$animate' , '$timeout' , '$rootScope' , '$q' , '$interpolate' ,
221
- function ( $scope , $exceptionHandler , $attr , $element , $parse , $animate , $timeout , $rootScope , $q , $interpolate ) {
221
+ var NgModelController = [ '$scope' , '$exceptionHandler' , '$attrs' , '$element' , '$parse' , '$animate' , '$timeout' , '$rootScope' , '$q' , '$interpolate' , '$modelOptions' ,
222
+ function ( $scope , $exceptionHandler , $attr , $element , $parse , $animate , $timeout , $rootScope , $q , $interpolate , $modelOptions ) {
222
223
this . $viewValue = Number . NaN ;
223
224
this . $modelValue = Number . NaN ;
224
225
this . $$rawModelValue = undefined ; // stores the parsed modelValue / model set from scope regardless of validity.
@@ -237,7 +238,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
237
238
this . $$success = { } ; // keep valid keys here
238
239
this . $pending = undefined ; // keep pending keys here
239
240
this . $name = $interpolate ( $attr . name || '' , false ) ( $scope ) ;
240
-
241
+ this . $options = $modelOptions . defaultOptions ;
241
242
242
243
var parsedNgModel = $parse ( $attr . ngModel ) ,
243
244
parsedNgModelAssign = parsedNgModel . assign ,
@@ -246,9 +247,10 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
246
247
pendingDebounce = null ,
247
248
ctrl = this ;
248
249
249
- this . $$setOptions = function ( options ) {
250
- ctrl . $options = options ;
251
- if ( options && options . getterSetter ) {
250
+
251
+ this . $$initGetterSetters = function ( ) {
252
+
253
+ if ( ctrl . $options . getterSetter ) {
252
254
var invokeModelGetter = $parse ( $attr . ngModel + '()' ) ,
253
255
invokeModelSetter = $parse ( $attr . ngModel + '($$$p)' ) ;
254
256
@@ -272,6 +274,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
272
274
}
273
275
} ;
274
276
277
+
275
278
/**
276
279
* @ngdoc method
277
280
* @name ngModel.NgModelController#$render
@@ -523,7 +526,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
523
526
var prevValid = ctrl . $valid ;
524
527
var prevModelValue = ctrl . $modelValue ;
525
528
526
- var allowInvalid = ctrl . $options && ctrl . $options . allowInvalid ;
529
+ var allowInvalid = ctrl . $options . allowInvalid ;
527
530
528
531
ctrl . $$runValidators ( parserValid , modelValue , viewValue , function ( allValid ) {
529
532
// If there was no change in validity, don't update the model
@@ -683,7 +686,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
683
686
ctrl . $modelValue = ngModelGet ( $scope ) ;
684
687
}
685
688
var prevModelValue = ctrl . $modelValue ;
686
- var allowInvalid = ctrl . $options && ctrl . $options . allowInvalid ;
689
+ var allowInvalid = ctrl . $options . allowInvalid ;
687
690
ctrl . $$rawModelValue = modelValue ;
688
691
689
692
if ( allowInvalid ) {
@@ -764,7 +767,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
764
767
*/
765
768
this . $setViewValue = function ( value , trigger ) {
766
769
ctrl . $viewValue = value ;
767
- if ( ! ctrl . $options || ctrl . $options . updateOnDefault ) {
770
+ if ( ctrl . $options . updateOnDefault ) {
768
771
ctrl . $$debounceViewValueCommit ( trigger ) ;
769
772
}
770
773
} ;
@@ -774,7 +777,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
774
777
options = ctrl . $options ,
775
778
debounce ;
776
779
777
- if ( options && isDefined ( options . debounce ) ) {
780
+ if ( isDefined ( options . debounce ) ) {
778
781
debounce = options . debounce ;
779
782
if ( isNumber ( debounce ) ) {
780
783
debounceDelay = debounce ;
@@ -1016,7 +1019,11 @@ var ngModelDirective = ['$rootScope', function($rootScope) {
1016
1019
var modelCtrl = ctrls [ 0 ] ,
1017
1020
formCtrl = ctrls [ 1 ] || nullFormCtrl ;
1018
1021
1019
- modelCtrl . $$setOptions ( ctrls [ 2 ] && ctrls [ 2 ] . $options ) ;
1022
+ if ( ctrls [ 2 ] ) {
1023
+ modelCtrl . $options = ctrls [ 2 ] . $options ;
1024
+ }
1025
+
1026
+ modelCtrl . $$initGetterSetters ( ) ;
1020
1027
1021
1028
// notify others, especially parent forms
1022
1029
formCtrl . $addControl ( modelCtrl ) ;
@@ -1033,7 +1040,7 @@ var ngModelDirective = ['$rootScope', function($rootScope) {
1033
1040
} ,
1034
1041
post : function ngModelPostLink ( scope , element , attr , ctrls ) {
1035
1042
var modelCtrl = ctrls [ 0 ] ;
1036
- if ( modelCtrl . $options && modelCtrl . $options . updateOn ) {
1043
+ if ( modelCtrl . $options . updateOn ) {
1037
1044
element . on ( modelCtrl . $options . updateOn , function ( ev ) {
1038
1045
modelCtrl . $$debounceViewValueCommit ( ev && ev . type ) ;
1039
1046
} ) ;
@@ -1396,3 +1403,45 @@ function isObjectEmpty(obj) {
1396
1403
}
1397
1404
return true ;
1398
1405
}
1406
+
1407
+
1408
+
1409
+ /**
1410
+ * @ngdoc service
1411
+ * @name $modelOptions
1412
+ * @description
1413
+ * This service provides support to the ngModelOptions directive.
1414
+ *
1415
+ * You can register default options that are used application wide if no ngModelOptions
1416
+ * directive is found.
1417
+ *
1418
+ * You can register model adaptors that can selected by ngModelOptions settings to modify
1419
+ * the behavior of ngModel, form and input directives.
1420
+ */
1421
+ function $ModelOptionsProvider ( ) {
1422
+ return {
1423
+ $get : function ( ) {
1424
+ return {
1425
+ /**
1426
+ * @ngdoc property
1427
+ * @name $modelOptions#defaultOptions
1428
+ * @type {Object }
1429
+ * @description
1430
+ * The default options to fall back on when there are no more ngModelOption
1431
+ * directives as ancestors
1432
+ */
1433
+ defaultOptions : {
1434
+ updateOnDefault : true
1435
+ } ,
1436
+ /**
1437
+ * @ngdoc property
1438
+ * @name $modelOptions#modelAdaptors
1439
+ * @type {Object<string, Function> }
1440
+ * @description
1441
+ * The functions that adapt the behaviour of ngModel
1442
+ */
1443
+ modelAdaptors : { }
1444
+ } ;
1445
+ }
1446
+ } ;
1447
+ } ;
0 commit comments