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

Commit 07477c3

Browse files
committed
docs(ngRepeat): add warning about track by $index with one-time bindings
1 parent 7368570 commit 07477c3

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/ng/directive/ngRepeat.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@
6262
* # Tracking and Duplicates
6363
*
6464
* `ngRepeat` uses {@link $rootScope.Scope#$watchCollection $watchCollection} to detect changes in
65-
* the collection. When a change happens, ngRepeat then makes the corresponding changes to the DOM:
65+
* the collection. When a change happens, `ngRepeat` then makes the corresponding changes to the DOM:
6666
*
6767
* * When an item is added, a new instance of the template is added to the DOM.
6868
* * When an item is removed, its template instance is removed from the DOM.
6969
* * When items are reordered, their respective templates are reordered in the DOM.
7070
*
7171
* To minimize creation of DOM elements, `ngRepeat` uses a function
7272
* to "keep track" of all items in the collection and their corresponding DOM elements.
73-
* For example, if an item is added to the collection, ngRepeat will know that all other items
73+
* For example, if an item is added to the collection, `ngRepeat` will know that all other items
7474
* already have DOM elements, and will not re-render them.
7575
*
7676
* The default tracking function (which tracks items by their identity) does not allow
@@ -97,19 +97,29 @@
9797
* ```
9898
*
9999
* <div class="alert alert-success">
100-
* If you are working with objects that have an identifier property, you should track
101-
* by the identifier instead of the whole object. Should you reload your data later, `ngRepeat`
100+
* If you are working with objects that have a unique identifier property, you should track
101+
* by this identifier instead of the object instance. Should you reload your data later, `ngRepeat`
102102
* will not have to rebuild the DOM elements for items it has already rendered, even if the
103103
* JavaScript objects in the collection have been substituted for new ones. For large collections,
104104
* this significantly improves rendering performance. If you don't have a unique identifier,
105105
* `track by $index` can also provide a performance boost.
106106
* </div>
107+
*
107108
* ```html
108109
* <div ng-repeat="model in collection track by model.id">
109110
* {{model.name}}
110111
* </div>
111112
* ```
112113
*
114+
* <br />
115+
* <div class="alert alert-warning">
116+
* Avoid using `track by $index` when the repeated template contains
117+
* {@link guide/expression#one-time-binding one-time bindings}. In such cases, the `nth` DOM
118+
* element will always be matched with the `nth` item of the array, so the bindings on that element
119+
* will not be updated even when the corresponding item changes, essentially causing the view to get
120+
* out-of-sync with the underlying data.
121+
* </div>
122+
*
113123
* When no `track by` expression is provided, it is equivalent to tracking by the built-in
114124
* `$id` function, which tracks items by their identity:
115125
* ```html
@@ -118,15 +128,17 @@
118128
* </div>
119129
* ```
120130
*
131+
* <br />
121132
* <div class="alert alert-warning">
122133
* **Note:** `track by` must always be the last expression:
123134
* </div>
124135
* ```
125-
* <div ng-repeat="model in collection | orderBy: 'id' as filtered_result track by model.id">
126-
* {{model.name}}
127-
* </div>
136+
* <div ng-repeat="model in collection | orderBy: 'id' as filtered_result track by model.id">
137+
* {{model.name}}
138+
* </div>
128139
* ```
129140
*
141+
*
130142
* # Special repeat start and end points
131143
* To repeat a series of elements instead of just one parent element, ngRepeat (as well as other ng directives) supports extending
132144
* the range of the repeater by defining explicit start and end points by using **ng-repeat-start** and **ng-repeat-end** respectively.

0 commit comments

Comments
 (0)