Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
docs(ngModelController): improve $rollbackViewValue description & exa…
Browse files Browse the repository at this point in the history
…mple

The example has been expanded to make it easier to provoke the
behavior that the description is talking about (rollbackViewValue
and programmatic model updates)

Related #13340
  • Loading branch information
Narretz committed Dec 3, 2015
1 parent 6628b4f commit 25e8c59
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions src/ng/directive/ngModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,14 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
* which may be caused by a pending debounced event or because the input is waiting for a some
* future event.
*
* If you have an input that uses `ng-model-options` to set up debounced events or events such
* as blur you can have a situation where there is a period when the `$viewValue`
* is out of synch with the ngModel's `$modelValue`.
* If you have an input that uses `ng-model-options` to set up debounced updates or updates that
* depend on special events such as blur, you can have a situation where there is a period when
* the `$viewValue` is out of sync with the ngModel's `$modelValue`.
*
* In this case, you can run into difficulties if you try to update the ngModel's `$modelValue`
* In this case, you can use `$rollbackViewValue()` to manually cancel the debounced / future update
* and reset the input to the last committed view value.
*
* It is also possible that you run into difficulties if you try to update the ngModel's `$modelValue`
* programmatically before these debounced/future events have resolved/occurred, because Angular's
* dirty checking mechanism is not able to tell whether the model has actually changed or not.
*
Expand All @@ -465,39 +468,63 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
* angular.module('cancel-update-example', [])
*
* .controller('CancelUpdateController', ['$scope', function($scope) {
* $scope.resetWithCancel = function(e) {
* if (e.keyCode == 27) {
* $scope.myForm.myInput1.$rollbackViewValue();
* $scope.myValue = '';
* }
* };
* $scope.resetWithoutCancel = function(e) {
* $scope.model = {};
*
* $scope.setEmpty = function(e, value, rollback) {
* if (e.keyCode == 27) {
* $scope.myValue = '';
* e.preventDefault();
* if (rollback) {
* $scope.myForm[value].$rollbackViewValue();
* }
* $scope.model[value] = '';
* }
* };
* }]);
* </file>
* <file name="index.html">
* <div ng-controller="CancelUpdateController">
* <p>Try typing something in each input. See that the model only updates when you
* blur off the input.
* </p>
* <p>Now see what happens if you start typing then press the Escape key</p>
* <p>Both of these inputs are only updated if they are blurred. Hitting escape should
* empty them. Follow these steps and observe the difference:</p>
* <ol>
* <li>Type something in the input. You will see that the model is not yet updated</li>
* <li>Press the Escape key.
* <ol>
* <li> In the first example, nothing happens, because the model is already '', and no
* update is detected. If you blur the input, the model will be set to the current view.
* </li>
* <li> In the second example, the pending update is cancelled, and the input is set back
* to the last committed view value (''). Blurring the input does nothing.
* </li>
* </ol>
* </li>
* </ol>
*
* <form name="myForm" ng-model-options="{ updateOn: 'blur' }">
* <p id="inputDescription1">With $rollbackViewValue()</p>
* <input name="myInput1" aria-describedby="inputDescription1" ng-model="myValue"
* ng-keydown="resetWithCancel($event)"><br/>
* myValue: "{{ myValue }}"
* <div>
* <p id="inputDescription1">Without $rollbackViewValue():</p>
* <input name="value1" aria-describedby="inputDescription1" ng-model="model.value1"
* ng-keydown="setEmpty($event, 'value1')">
* value1: "{{ model.value1 }}"
* </div>
*
* <p id="inputDescription2">Without $rollbackViewValue()</p>
* <input name="myInput2" aria-describedby="inputDescription2" ng-model="myValue"
* ng-keydown="resetWithoutCancel($event)"><br/>
* myValue: "{{ myValue }}"
* <div>
* <p id="inputDescription2">With $rollbackViewValue():</p>
* <input name="value2" aria-describedby="inputDescription2" ng-model="model.value2"
* ng-keydown="setEmpty($event, 'value2', true)">
* value2: "{{ model.value2 }}"
* </div>
* </form>
* </div>
* </file>
<file name="style.css">
div {
display: table-cell;
}
div:nth-child(1) {
padding-right: 30px;
}
</file>
* </example>
*/
this.$rollbackViewValue = function() {
Expand Down

0 comments on commit 25e8c59

Please sign in to comment.