Skip to content

Commit faec997

Browse files
docs(ngModelController): add example for $cancelUpdate
1 parent 66a1328 commit faec997

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

src/ng/directive/input.js

+53-12
Original file line numberDiff line numberDiff line change
@@ -1703,13 +1703,53 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
17031703
*
17041704
* If you have an input that uses `ng-model-options` to set up debounced events or events such
17051705
* as blur you can have a situation where there is a period when the value of the input element
1706-
* is out of synch with the ngModel's `$viewValue`. You can run into difficulties if you try to
1707-
* update the ngModel's `$modelValue` programmatically before these debounced/future events have
1708-
* completed, because Angular's dirty checking mechanism is not able to tell whether the model
1709-
* has actually changed or not. This method should be called before directly updating a model
1710-
* from the scope in case you have an input with `ng-model-options` that do not include immediate
1711-
* update of the default trigger. This is important in order to make sure that this input field
1712-
* will be updated with the new value and any pending operation will be canceled.
1706+
* is out of synch with the ngModel's `$viewValue`.
1707+
*
1708+
* In this case, you can run into difficulties if you try to update the ngModel's `$modelValue`
1709+
* programmatically before these debounced/future events have resolved/occurred, because Angular's
1710+
* dirty checking mechanism is not able to tell whether the model has actually changed or not.
1711+
*
1712+
* The `$cancelUpdate()` method should be called before programmatically changing the model of an
1713+
* input which may have such events pending. This is important in order to make sure that the
1714+
* input field will be updated with the new model value and any pending operations are cancelled.
1715+
*
1716+
* <example name="ng-model-cancel-update" module="cancel-update-example">
1717+
* <file name="app.js">
1718+
* angular.module('cancel-update-example', [])
1719+
*
1720+
* .controller('CancelUpdateCtrl', function($scope) {
1721+
* $scope.resetWithCancel = function (e) {
1722+
* if (e.keyCode == 27) {
1723+
* $scope.myForm.myInput1.$cancelUpdate();
1724+
* $scope.myValue = '';
1725+
* }
1726+
* };
1727+
* $scope.resetWithoutCancel = function (e) {
1728+
* if (e.keyCode == 27) {
1729+
* $scope.myValue = '';
1730+
* }
1731+
* };
1732+
* });
1733+
* </file>
1734+
* <file name="index.html">
1735+
* <div ng-controller="CancelUpdateCtrl">
1736+
* <p>Try typing something in each input. See that the model only updates when you
1737+
* blur off the input.
1738+
* </p>
1739+
* <p>Now see what happens if you start typing then press the Escape key</p>
1740+
*
1741+
* <form name="myForm" ng-model-options="{ updateOn: 'blur' }">
1742+
* <p>With $cancelUpdate()</p>
1743+
* <input name="myInput1" ng-model="myValue" ng-keydown="resetWithCancel($event)"><br/>
1744+
* myValue: "{{ myValue }}"
1745+
*
1746+
* <p>Without $cancelUpdate()</p>
1747+
* <input name="myInput2" ng-model="myValue" ng-keydown="resetWithoutCancel($event)"><br/>
1748+
* myValue: "{{ myValue }}"
1749+
* </form>
1750+
* </div>
1751+
* </file>
1752+
* </example>
17131753
*/
17141754
this.$cancelUpdate = function() {
17151755
$timeout.cancel(pendingDebounce);
@@ -2216,12 +2256,13 @@ var ngValueDirective = function() {
22162256
*
22172257
* Given the nature of `ngModelOptions`, the value displayed inside input fields in the view might
22182258
* 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.
2259+
* should also invoke {@link ngModel.NgModelController `$cancelUpdate`} on the relevant input field in
2260+
* order to make sure it is synchronized with the model and that any debounced action is canceled.
22212261
*
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.
2262+
* The easiest way to reference the control's {@link ngModel.NgModelController `$cancelUpdate`}
2263+
* method is by making sure the input is placed inside a form that has a `name` attribute. This is
2264+
* important because `form` controllers are published to the related scope under the name in their
2265+
* `name` attribute.
22252266
*
22262267
* @param {Object} ngModelOptions options to apply to the current model. Valid keys are:
22272268
* - `updateOn`: string specifying which event should be the input bound to. You can set several

0 commit comments

Comments
 (0)