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

Commit 2e84cf9

Browse files
docs(ngModel): further mods to $viewValue semantics
Closes #8110
1 parent ada9852 commit 2e84cf9

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

src/ng/directive/input.js

+31-11
Original file line numberDiff line numberDiff line change
@@ -1577,9 +1577,19 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
15771577
*
15781578
* @description
15791579
* Called when the view needs to be updated. It is expected that the user of the ng-model
1580-
* directive will implement this method. Note that in case `$modelValue` is an object,
1581-
* this method will be invoked only if a different instance is assigned to the model since
1582-
* `ngModel` does not perform a deep watch of objects.
1580+
* directive will implement this method.
1581+
*
1582+
* The `$render()` method is invoked in the following situations:
1583+
*
1584+
* * `$rollbackViewValue()` is called. If we are rolling back the view value to the last
1585+
* committed value then `$render()` is called to update the input control.
1586+
* * The value referenced by `ng-model` is changed programmatically and both the `$modelValue` and
1587+
* the `$viewValue` are different to last time.
1588+
*
1589+
* Since `ng-model` does not do a deep watch, `$render()` is only invoked if the values of
1590+
* `$modelValue` and `$viewValue` are actually different to their previous value. If `$modelValue`
1591+
* or `$viewValue` are objects (rather than a string or number) then `$render()` will not be
1592+
* invoked if you only change a property on the objects.
15831593
*/
15841594
this.$render = noop;
15851595

@@ -1880,15 +1890,25 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
18801890
* @description
18811891
* Update the view value.
18821892
*
1883-
* This method should be called when the view value changes, typically from within a DOM event handler.
1884-
* For example {@link ng.directive:input input} and {@link ng.directive:select select}
1885-
* directives call it. In case the view value is an object, this method should be called with a
1886-
* copy of that object since `ngModel` does not perform a deep watch of objects. Properties of
1887-
* that copy should not be changed after `$setViewValue` in invoked.
1893+
* This method should be called when an input directive want to change the view value; typically,
1894+
* this is done from within a DOM event handler.
1895+
*
1896+
* For example {@link ng.directive:input input} calls it when the value of the input changes and
1897+
* {@link ng.directive:select select} calls it when an option is selected.
1898+
*
1899+
* If the new `value` is an object (rather than a string or a number), we should make a copy of the
1900+
* object before passing it to `$setViewValue`. This is because `ngModel` does not perform a deep
1901+
* watch of objects, it only looks for a change of identity. If you only change the property of
1902+
* the object then ngModel will not realise that the object has changed and will not invoke the
1903+
* `$parsers` and `$validators` pipelines.
1904+
*
1905+
* For this reason, you should not change properties of the copy once it has been passed to
1906+
* `$setViewValue`. Otherwise you may cause the model value on the scope to change incorrectly.
18881907
*
1889-
* It will update the $viewValue, then pass this value through each of the functions in `$parsers`,
1890-
* which includes any validators. The value that comes out of this `$parsers` pipeline, be applied to
1891-
* `$modelValue` and the **expression** specified in the `ng-model` attribute.
1908+
* When this method is called, the new `value` will be staged for committing through the `$parsers`
1909+
* and `$validators` pipelines. If there are no special {@link ngModelOptions} specified then the staged
1910+
* value sent directly for processing, finally to be applied to `$modelValue` and then the
1911+
* **expression** specified in the `ng-model` attribute.
18921912
*
18931913
* Lastly, all the registered change listeners, in the `$viewChangeListeners` list, are called.
18941914
*

0 commit comments

Comments
 (0)