Skip to content

Commit 04d4d93

Browse files
Thesoropetebacondarwin
authored andcommitted
docs(ngModel): add section explaining that ngModel watches by reference
The new section explains that changing only a property on an object doesn't trigger re-rendering. Closes angular#13224 Closes angular#13518
1 parent 8348365 commit 04d4d93

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/ng/directive/ngModel.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,22 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
937937
* - {@link ng.directive:select select}
938938
* - {@link ng.directive:textarea textarea}
939939
*
940+
* # Complex Models (objects or collections)
941+
*
942+
* By default, `ngModel` watches the model by reference, not value. This is important to know when
943+
* binding inputs to models that are objects (e.g. `Date`) or collections (e.g. arrays). If only properties of the
944+
* object or collection change, `ngModel` will not be notified and so the input will not be re-rendered.
945+
*
946+
* The model must be assigned an entirely new object or collection before a re-rendering will occur.
947+
*
948+
* Some directives have options that will cause them to use a custom `$watchCollection` on the model expression
949+
* - for example, `ngOptions` will do so when a `track by` clause is included in the comprehension expression or
950+
* if the select is given the `multiple` attribute.
951+
*
952+
* The `$watchCollection()` method only does a shallow comparison, meaning that changing properties deeper than the
953+
* first level of the object (or only changing the properties of an item in the collection if it's an array) will still
954+
* not trigger a re-rendering of the model.
955+
*
940956
* # CSS classes
941957
* The following CSS classes are added and removed on the associated input/select/textarea element
942958
* depending on the validity of the model.

0 commit comments

Comments
 (0)