Skip to content

Commit f9eb1d8

Browse files
committed
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. Closes angular#1285, angular#2129
1 parent c704f76 commit f9eb1d8

File tree

3 files changed

+491
-68
lines changed

3 files changed

+491
-68
lines changed

docs/content/guide/forms.ngdoc

+63
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,69 @@ This allows us to extend the above example with these features:
181181

182182

183183

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
197+
some specific circumstances (like blur events).
198+
199+
I.e. `ng-model-options="{ updateOn: ["default", "blur"], debounce: { default: 500, blur: 0 } }"`
200+
201+
If those attributes are added to an element, they will be applied to all the child elements and controls that inherit from it unless they are
202+
overriden.
203+
204+
The following example shows how to override immediate updates. Changes on the inputs within the form will update the model
205+
only when the control loses focus (blur event).
206+
207+
<example>
208+
<file name="index.html">
209+
<div ng-controller="ControllerUpdateOn">
210+
<form name="form" class="css-form">
211+
Name:
212+
<input type="text" ng-model="username" ng-model-options="{ updateOn: "blur" }" name="uName" /><br />
213+
Other data:
214+
<input type="text" ng-model="userdata" name="uData" /><br />
215+
</form>
216+
<pre>username = "{{username}}"</pre>
217+
</div>
218+
</file>
219+
<file name="script.js">
220+
function ControllerUpdateOn($scope) {
221+
$scope.username = "";
222+
}
223+
</file>
224+
</example>
225+
226+
This one shows how to debounce model changes. Model will be updated only 250 milliseconds after last change.
227+
228+
<example>
229+
<file name="index.html">
230+
<div ng-controller="ControllerUpdateOn">
231+
<form name="form" class="css-form">
232+
Name:
233+
<input type="text" ng-model="username" name="uName" ng-model-options="{ debounce: 250 }" /><br />
234+
</form>
235+
<pre>username = "{{user.name}}"</pre>
236+
</div>
237+
</file>
238+
<file name="script.js">
239+
function ControllerUpdateOn($scope) {
240+
$scope.username = "";
241+
}
242+
</file>
243+
</example>
244+
245+
246+
184247
# Custom Validation
185248

186249
Angular provides basic implementation for most common html5 {@link ng.directive:input input}

0 commit comments

Comments
 (0)