Skip to content

Commit 66a1328

Browse files
shahatapetebacondarwin
authored andcommitted
docs(ngModelOptions): add some docs for $cancelUpdate
1 parent 2354924 commit 66a1328

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

src/ng/directive/input.js

+47-15
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,15 @@ var ngValueDirective = function() {
22142214
* events that will trigger a model update and/or a debouncing delay so that the actual update only
22152215
* takes place when a timer expires; this timer will be reset after another change takes place.
22162216
*
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+
*
22172226
* @param {Object} ngModelOptions options to apply to the current model. Valid keys are:
22182227
* - `updateOn`: string specifying which event should be the input bound to. You can set several
22192228
* events using an space delimited list. There is a special event called `default` that
@@ -2226,49 +2235,72 @@ var ngValueDirective = function() {
22262235
* @example
22272236
22282237
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.
22302240
22312241
<example name="ngModelOptions-directive-blur">
22322242
<file name="index.html">
22332243
<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>
22422254
<pre>user.name = <span ng-bind="user.name"></span></pre>
22432255
</div>
22442256
</file>
22452257
<file name="app.js">
22462258
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+
};
22482266
}
22492267
</file>
22502268
<file name="protractor.js" type="protractor">
22512269
var model = element(by.binding('user.name'));
22522270
var input = element(by.model('user.name'));
22532271
var other = element(by.model('user.data'));
2272+
22542273
it('should allow custom events', function() {
22552274
input.sendKeys(' hello');
22562275
expect(model.getText()).toEqual('say');
22572276
other.click();
22582277
expect(model.getText()).toEqual('say hello');
22592278
});
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+
});
22602288
</file>
22612289
</example>
22622290
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.
22642293
22652294
<example name="ngModelOptions-directive-debounce">
22662295
<file name="index.html">
22672296
<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>
22722304
<pre>user.name = <span ng-bind="user.name"></span></pre>
22732305
</div>
22742306
</file>

0 commit comments

Comments
 (0)