@@ -177,6 +177,8 @@ defaultModelOptions = new ModelOptions({
177
177
* `submit` event. Note that `ngClick` events will occur before the model is updated. Use `ngSubmit`
178
178
* to have access to the updated model.
179
179
*
180
+ * ### Overriding immediate updates
181
+ *
180
182
* The following example shows how to override immediate updates. Changes on the inputs within the
181
183
* form will update the model only when the control loses focus (blur event). If `escape` key is
182
184
* pressed while the input field is focused, the value is reset to the value in the current model.
@@ -236,6 +238,8 @@ defaultModelOptions = new ModelOptions({
236
238
* </file>
237
239
* </example>
238
240
*
241
+ * ### Debouncing updates
242
+ *
239
243
* The next example shows how to debounce model changes. Model will be updated only 1 sec after last change.
240
244
* If the `Clear` button is pressed, any debounced action is canceled and the value becomes empty.
241
245
*
@@ -260,6 +264,106 @@ defaultModelOptions = new ModelOptions({
260
264
* </file>
261
265
* </example>
262
266
*
267
+ * ### Default events, extra triggers, and catch-all debounce values
268
+ *
269
+ * This example shows the relationship between "default" update events and
270
+ * additional `updateOn` triggers.
271
+ *
272
+ * `default` events are those that are bound to the control, and when fired, update the `$viewValue`
273
+ * via {@link ngModel.NgModelController#$setViewValue $setViewValue}. Every event that is not listed
274
+ * in `updateOn` is considered a "default" event, since different control types have different
275
+ * default events.
276
+ *
277
+ * The control in this example updates by "default", "click", and "blur", with different `debounce`
278
+ * values. You can see that "click" doesn't have an individual `debounce` value -
279
+ * therefore it uses the `*` debounce value.
280
+ *
281
+ * There is also a button that calls {@link ngModel.NgModelController#$setViewValue $setViewValue}
282
+ * directly with a "custom" event. Since "custom" is not defined in the `updateOn` list,
283
+ * it is considered a "default" event and will update the
284
+ * control if "default" is defined in `updateOn`, and will receive the "default" debounce value.
285
+ * Note that this is just to illustrate how custom controls would possibly call `$setViewValue`.
286
+ *
287
+ * You can change the `updateOn` and `debounce` configuration to test different scenarios. This
288
+ * is done with {@link ngModel.NgModelController#$overrideModelOptions $overrideModelOptions}.
289
+ *
290
+ <example name="ngModelOptions-advanced" module="optionsExample">
291
+ <file name="index.html">
292
+ <model-update-demo></model-update-demo>
293
+ </file>
294
+ <file name="app.js">
295
+ angular.module('optionsExample', [])
296
+ .component('modelUpdateDemo', {
297
+ templateUrl: 'template.html',
298
+ controller: function() {
299
+ this.name = 'Chinua';
300
+
301
+ this.options = {
302
+ updateOn: 'default blur click',
303
+ debounce: {
304
+ default: 2000,
305
+ blur: 0,
306
+ '*': 1000
307
+ }
308
+ };
309
+
310
+ this.updateEvents = function() {
311
+ var eventList = this.options.updateOn.split(' ');
312
+ eventList.push('*');
313
+ var events = {};
314
+
315
+ for (var i = 0; i < eventList.length; i++) {
316
+ events[eventList[i]] = this.options.debounce[eventList[i]];
317
+ }
318
+
319
+ this.events = events;
320
+ };
321
+
322
+ this.updateOptions = function() {
323
+ var options = angular.extend(this.options, {
324
+ updateOn: Object.keys(this.events).join(' ').replace('*', ''),
325
+ debounce: this.events
326
+ });
327
+
328
+ this.form.input.$overrideModelOptions(options);
329
+ };
330
+
331
+ // Initialize the event form
332
+ this.updateEvents();
333
+ }
334
+ });
335
+ </file>
336
+ <file name="template.html">
337
+ <form name="$ctrl.form">
338
+ Input: <input type="text" name="input" ng-model="$ctrl.name" ng-model-options="$ctrl.options" />
339
+ </form>
340
+ Model: <tt>{{$ctrl.name}}</tt>
341
+ <hr>
342
+ <button ng-click="$ctrl.form.input.$setViewValue('some value', 'custom')">Trigger setViewValue with 'some value' and 'custom' event</button>
343
+
344
+ <hr>
345
+ <form ng-submit="$ctrl.updateOptions()">
346
+ <b>updateOn</b><br>
347
+ <input type="text" ng-model="$ctrl.options.updateOn" ng-change="$ctrl.updateEvents()" ng-model-options="{debounce: 500}">
348
+
349
+ <table>
350
+ <tr>
351
+ <th>Option</th>
352
+ <th>Debounce value</th>
353
+ </tr>
354
+ <tr ng-repeat="(key, value) in $ctrl.events">
355
+ <td>{{key}}</td>
356
+ <td><input type="number" ng-model="$ctrl.events[key]" /></td>
357
+ </tr>
358
+ </table>
359
+
360
+ <br>
361
+ <input type="submit" value="Update options">
362
+ </form>
363
+ </file>
364
+ </example>
365
+ *
366
+ *
263
367
* ## Model updates and validation
264
368
*
265
369
* The default behaviour in `ngModel` is that the model value is set to `undefined` when the
@@ -307,11 +411,30 @@ defaultModelOptions = new ModelOptions({
307
411
* You can specify the timezone that date/time input directives expect by providing its name in the
308
412
* `timezone` property.
309
413
*
414
+ *
415
+ * ## Programmatically changing options
416
+ *
417
+ * The `ngModelOptions` expression is only evaluated once when the directive is linked; it is not
418
+ * watched for changes. However, it is possible to override the options on a single
419
+ * {@link ngModel.NgModelController } instance with
420
+ * {@link ngModel.NgModelController#$overrideModelOptions }. See also the example for
421
+ * {@link ngModelOptions#default-events-extra-triggers-and-catch-all-debounce-values
422
+ * Default events, extra triggers, and catch-all debounce values}.
423
+ *
424
+ *
310
425
* @param {Object } ngModelOptions options to apply to {@link ngModel} directives on this element and
311
426
* and its descendents. Valid keys are:
312
427
* - `updateOn`: string specifying which event should the input be bound to. You can set several
313
428
* events using an space delimited list. There is a special event called `default` that
314
- * matches the default events belonging to the control.
429
+ * matches the default events belonging to the control. These are the events that are bound to
430
+ * the control, and when fired, update the `$viewValue` via `$setViewValue`.
431
+ *
432
+ * `ngModelOptions` considers every event that is not listed in `updateOn` a "default" event,
433
+ * since different control types use different default events.
434
+ *
435
+ * See also the section {@link ngModelOptions#triggering-and-debouncing-model-updates
436
+ * Triggering and debouncing model updates}.
437
+ *
315
438
* - `debounce`: integer value which contains the debounce model update value in milliseconds. A
316
439
* value of 0 triggers an immediate update. If an object is supplied instead, you can specify a
317
440
* custom value for each event. For example:
0 commit comments