You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(ngModelOptions): Model update behavior can now be customized
By default, any change on the content will trigger an immediate model update
and form validation. This PR implements a new directive `ng-model-options`
that allow you to override this default behavior in several ways.
You should specify an object with the different parameters.
For example, it allows to trigger an update only when a particular event or
list of events is received by the input using the `updateOn` key. Should you
need multiple events, just assign an array to it.
I.e. `ng-model-options="{ updateOn: 'blur' }"` will update and validate only
after the control loses focus.
If you want to keep the default behavior and just add new events that may
trigger the model update and validation, add "default" as one of the specified
events.
I.e. `ng-model-options="{ updateOn: ['default','submit'] }"`
Also, with the `debounce` option, `ng-model-options` will allow defering the
actual model update until a timer expires. The timer will be reset each time
an event is triggered.
I.e. `ng-model-options="{ debounce: 500 }" for 500ms after the latest event.
Custom timeouts for each event can be set for each event if you use an object
in `debounce`. This can be useful to force immediate updates on some specific
circumstances (like blur events).
I.e. `ng-model-options="{ updateOn: ['default', 'blur'], debounce: { default: 500, blur: 0} }"`
You can use the directive in any tag so its contents became the default settings
for any child control, although they can be overriden.
Closesangular#1285, angular#2129
Copy file name to clipboardexpand all lines: docs/content/guide/forms.ngdoc
+63
Original file line number
Diff line number
Diff line change
@@ -181,6 +181,69 @@ This allows us to extend the above example with these features:
181
181
182
182
183
183
184
+
# Non-immediate (debounced) or custom triggered model updates
185
+
186
+
By default, any change on the content will trigger a model update and form validation. You can override this behavior using the {@link ng.directive:ngModelOptions ngModelOptions}
187
+
directive to bind only to specified list of events. I.e. `ng-model-options="{ updateOn: "blur" }"` will update and validate only after the control loses
188
+
focus. You can set a single event using an array instead of a string. I.e. `ng-model-options="{ updateOn: ["mousedown", "blur"] }"`
189
+
190
+
If you want to keep the default behavior and just add new events that may trigger the model update
191
+
and validation, add "default" as one of the specified events. I.e. `ng-model-options="{ updateOn: ["default", "blur"] }"`
192
+
193
+
You can delay the model update/validation by writing a `debounce` key. I.e. `ng-model-options="{ debounce: 500 }"` will wait for half a second since
194
+
the last content change before triggering the model update and form validation.
195
+
196
+
Custom debouncing timeouts can be set for each event for each event if you use an object in `debounce`. This can be useful to force immediate updates on
0 commit comments