Improve this Doc View Source ngModelOptions
- directive in module ng
Overview
This directive allows you to modify the behaviour of ngModel
directives within your application. You can specify an ngModelOptions
directive on any element. All ngModel
directives will use the options of their nearest ngModelOptions
ancestor.
The ngModelOptions
settings are found by evaluating the value of the attribute directive as an AngularJS expression. This expression should evaluate to an object, whose properties contain the settings. For example: <div ng-model-options="{ debounce: 100 }"
.
Inheriting Options
You can specify that an ngModelOptions
setting should be inherited from a parent ngModelOptions
directive by giving it the value of "$inherit"
. Then it will inherit that setting from the first ngModelOptions
directive found by traversing up the DOM tree. If there is no ancestor element containing an ngModelOptions
directive then default settings will be used.
For example given the following fragment of HTML
<div ng-model-options="{ allowInvalid: true, debounce: 200 }"> <form ng-model-options="{ updateOn: 'blur', allowInvalid: '$inherit' }"> <input ng-model-options="{ updateOn: 'default', allowInvalid: '$inherit' }" /> </form> </div>
the input
element will have the following settings
{ allowInvalid: true, updateOn: 'default', debounce: 0 }
Notice that the debounce
setting was not inherited and used the default value instead.
You can specify that all undefined settings are automatically inherited from an ancestor by including a property with key of "*"
and value of "$inherit"
.
For example given the following fragment of HTML
<div ng-model-options="{ allowInvalid: true, debounce: 200 }"> <form ng-model-options="{ updateOn: 'blur', "*": '$inherit' }"> <input ng-model-options="{ updateOn: 'default', "*": '$inherit' }" /> </form> </div>
the input
element will have the following settings
{ allowInvalid: true, updateOn: 'default', debounce: 200 }
Notice that the debounce
setting now inherits the value from the outer <div>
element.
If you are creating a reusable component then you should be careful when using "*": "$inherit"
since you may inadvertently inherit a setting in the future that changes the behavior of your component.
Triggering and debouncing model updates
The updateOn
and debounce
properties allow you to specify a custom list of events that will trigger a model update and/or a debouncing delay so that the actual update only takes place when a timer expires; this timer will be reset after another change takes place.
Given the nature of ngModelOptions
, the value displayed inside input fields in the view might be different from the value in the actual model. This means that if you update the model you should also invoke ngModel.NgModelController
on the relevant input field in order to make sure it is synchronized with the model and that any debounced action is canceled.
The easiest way to reference the control's ngModel.NgModelController
method is by making sure the input is placed inside a form that has a name
attribute. This is important because form
controllers are published to the related scope under the name in their name
attribute.
Any pending changes will take place immediately when an enclosing form is submitted via the submit
event. Note that ngClick
events will occur before the model is updated. Use ngSubmit
to have access to the updated model.
Overriding immediate updates
The following example shows how to override immediate updates. Changes on the inputs within the form will update the model only when the control loses focus (blur event). If escape
key is pressed while the input field is focused, the value is reset to the value in the current model.
Debouncing updates
The next example shows how to debounce model changes. Model will be updated only 1 sec after last change. If the Clear
button is pressed, any debounced action is canceled and the value becomes empty.
Model updates and validation
The default behaviour in ngModel
is that the model value is set to undefined
when the validation determines that the value is invalid. By setting the allowInvalid
property to true, the model will still be updated even if the value is invalid.
Connecting to the scope
By setting the getterSetter
property to true you are telling ngModel that the ngModel
expression on the scope refers to a "getter/setter" function rather than the value itself.
The following example shows how to bind to getter/setters:
Specifying timezones
You can specify the timezone that date/time input directives expect by providing its name in the timezone
property.
Programmatically changing options
The ngModelOptions
expression is only evaluated once when the directive is linked; it is not watched for changes. However, it is possible to override the options on a single ngModel.NgModelController
instance with NgModelController#$overrideModelOptions()
.
Directive Info
- This directive executes at priority level 10.
Usage
- as attribute:
<ANY ng-model-options="Object"> ... </ANY>
Arguments
Param | Type | Details |
---|---|---|
ngModelOptions | Object | options to apply to
|
© 2010–2018 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.6.9/docs/api/ng/directive/ngModelOptions