@@ -2214,6 +2214,15 @@ var ngValueDirective = function() {
2214
2214
* events that will trigger a model update and/or a debouncing delay so that the actual update only
2215
2215
* takes place when a timer expires; this timer will be reset after another change takes place.
2216
2216
*
2217
+ * Given the nature of `ngModelOptions`, the value displayed inside input fields in the view might
2218
+ * be different then the value in the actual model. This means that if you update the model you
2219
+ * should also invoke `$cancelUpdate` on the relevant input field in order to make sure it is
2220
+ * synchronized with the model and that any debounced action is canceled.
2221
+ *
2222
+ * The easiest way to reference the control's `$cancelUpdate` method is by making sure the input
2223
+ * is placed inside a form that has a `name` attribute. This is important because form controllers
2224
+ * are published to the related scope under the name in their `name` attribute.
2225
+ *
2217
2226
* @param {Object } ngModelOptions options to apply to the current model. Valid keys are:
2218
2227
* - `updateOn`: string specifying which event should be the input bound to. You can set several
2219
2228
* events using an space delimited list. There is a special event called `default` that
@@ -2226,49 +2235,72 @@ var ngValueDirective = function() {
2226
2235
* @example
2227
2236
2228
2237
The following example shows how to override immediate updates. Changes on the inputs within the
2229
- form will update the model only when the control loses focus (blur event).
2238
+ form will update the model only when the control loses focus (blur event). If `escape` key is
2239
+ pressed while the input field is focused, the value is reset to the value in the current model.
2230
2240
2231
2241
<example name="ngModelOptions-directive-blur">
2232
2242
<file name="index.html">
2233
2243
<div ng-controller="Ctrl">
2234
- Name:
2235
- <input type="text"
2236
- ng-model="user.name"
2237
- ng-model-options="{ updateOn: 'blur' }" /><br />
2238
-
2239
- Other data:
2240
- <input type="text" ng-model="user.data" /><br />
2241
-
2244
+ <form name="userForm">
2245
+ Name:
2246
+ <input type="text" name="userName"
2247
+ ng-model="user.name"
2248
+ ng-model-options="{ updateOn: 'blur' }"
2249
+ ng-keyup="cancel($event)" /><br />
2250
+
2251
+ Other data:
2252
+ <input type="text" ng-model="user.data" /><br />
2253
+ </form>
2242
2254
<pre>user.name = <span ng-bind="user.name"></span></pre>
2243
2255
</div>
2244
2256
</file>
2245
2257
<file name="app.js">
2246
2258
function Ctrl($scope) {
2247
- $scope.user = { name: 'say', data: '' };
2259
+ $scope.user = { name: 'say', data: '' };
2260
+
2261
+ $scope.cancel = function (e) {
2262
+ if (e.keyCode == 27) {
2263
+ $scope.userForm.userName.$cancelUpdate();
2264
+ }
2265
+ };
2248
2266
}
2249
2267
</file>
2250
2268
<file name="protractor.js" type="protractor">
2251
2269
var model = element(by.binding('user.name'));
2252
2270
var input = element(by.model('user.name'));
2253
2271
var other = element(by.model('user.data'));
2272
+
2254
2273
it('should allow custom events', function() {
2255
2274
input.sendKeys(' hello');
2256
2275
expect(model.getText()).toEqual('say');
2257
2276
other.click();
2258
2277
expect(model.getText()).toEqual('say hello');
2259
2278
});
2279
+
2280
+ it('should $cancelUpdate when model changes', function() {
2281
+ input.sendKeys(' hello');
2282
+ expect(input.getAttribute('value')).toEqual('say hello');
2283
+ input.sendKeys(protractor.Key.ESCAPE);
2284
+ expect(input.getAttribute('value')).toEqual('say');
2285
+ other.click();
2286
+ expect(model.getText()).toEqual('say');
2287
+ });
2260
2288
</file>
2261
2289
</example>
2262
2290
2263
- This one shows how to debounce model changes. Model will be updated only 500 milliseconds after last change.
2291
+ This one shows how to debounce model changes. Model will be updated only 1 sec after last change.
2292
+ If the `Clear` button is pressed, any debounced action is canceled and the value becomes empty.
2264
2293
2265
2294
<example name="ngModelOptions-directive-debounce">
2266
2295
<file name="index.html">
2267
2296
<div ng-controller="Ctrl">
2268
- Name:
2269
- <input type="text"
2270
- ng-model="user.name"
2271
- ng-model-options="{ debounce: 500 }" /><br />
2297
+ <form name="userForm">
2298
+ Name:
2299
+ <input type="text" name="userName"
2300
+ ng-model="user.name"
2301
+ ng-model-options="{ debounce: 1000 }" />
2302
+ <button ng-click="userForm.userName.$cancelUpdate(); user.name=''">Clear</button><br />
2303
+ </form>
2272
2304
<pre>user.name = <span ng-bind="user.name"></span></pre>
2273
2305
</div>
2274
2306
</file>
0 commit comments