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

Commit d8832d5

Browse files
teropaNarretz
authored andcommitted
docs(ngRepeat): extend description of tracking and duplicates
Add a section to the documentation on how tracking between items and DOM elements is done, and why duplicates are not allowed in the collection. Describe how the default tracking behaviour can be substituted with track by. Tweak the wording in the `track by` section to discuss “tracking expressions” instead of “tracking functions”. Closes #8153
1 parent f6d0ac5 commit d8832d5

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

src/ng/directive/ngRepeat.js

+55-6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,55 @@
4848
* or implement a `$watch` on the object yourself.
4949
*
5050
*
51+
* # Tracking and Duplicates
52+
*
53+
* When the contents of the collection change, `ngRepeat` makes the corresponding changes to the DOM:
54+
*
55+
* * When an item is added, a new instance of the template is added to the DOM.
56+
* * When an item is removed, its template instance is removed from the DOM.
57+
* * When items are reordered, their respective templates are reordered in the DOM.
58+
*
59+
* By default, `ngRepeat` does not allow duplicate items in arrays. This is because when
60+
* there are duplicates, it is not possible to maintain a one-to-one mapping between collection
61+
* items and DOM elements.
62+
*
63+
* If you do need to repeat duplicate items, you can substitute the default tracking behavior
64+
* with your own using the `track by` expression.
65+
*
66+
* For example, you may track items by the index of each item in the collection, using the
67+
* special scope property `$index`:
68+
* ```html
69+
* <div ng-repeat="n in [42, 42, 43, 43] track by $index">
70+
* {{n}}
71+
* </div>
72+
* ```
73+
*
74+
* You may use arbitrary expressions in `track by`, including references to custom functions
75+
* on the scope:
76+
* ```html
77+
* <div ng-repeat="n in [42, 42, 43, 43] track by myTrackingFunction(n)">
78+
* {{n}}
79+
* </div>
80+
* ```
81+
*
82+
* If you are working with objects that have an identifier property, you can track
83+
* by the identifier instead of the whole object. Should you reload your data later, `ngRepeat`
84+
* will not have to rebuild the DOM elements for items it has already rendered, even if the
85+
* JavaScript objects in the collection have been substituted for new ones:
86+
* ```html
87+
* <div ng-repeat="model in collection track by model.id">
88+
* {{model.name}}
89+
* </div>
90+
* ```
91+
*
92+
* When no `track by` expression is provided, it is equivalent to tracking by the built-in
93+
* `$id` function, which tracks items by their identity:
94+
* ```html
95+
* <div ng-repeat="obj in collection track by $id(obj)">
96+
* {{obj.prop}}
97+
* </div>
98+
* ```
99+
*
51100
* # Special repeat start and end points
52101
* To repeat a series of elements instead of just one parent element, ngRepeat (as well as other ng directives) supports extending
53102
* the range of the repeater by defining explicit start and end points by using **ng-repeat-start** and **ng-repeat-end** respectively.
@@ -115,12 +164,12 @@
115164
*
116165
* For example: `(name, age) in {'adam':10, 'amalie':12}`.
117166
*
118-
* * `variable in expression track by tracking_expression` – You can also provide an optional tracking function
119-
* which can be used to associate the objects in the collection with the DOM elements. If no tracking function
120-
* is specified the ng-repeat associates elements by identity in the collection. It is an error to have
121-
* more than one tracking function to resolve to the same key. (This would mean that two distinct objects are
122-
* mapped to the same DOM element, which is not possible.) Filters should be applied to the expression,
123-
* before specifying a tracking expression.
167+
* * `variable in expression track by tracking_expression` – You can also provide an optional tracking expression
168+
* which can be used to associate the objects in the collection with the DOM elements. If no tracking expression
169+
* is specified, ng-repeat associates elements by identity. It is an error to have
170+
* more than one tracking expression value resolve to the same key. (This would mean that two distinct objects are
171+
* mapped to the same DOM element, which is not possible.) If filters are used in the expression, they should be
172+
* applied before the tracking expression.
124173
*
125174
* For example: `item in items` is equivalent to `item in items track by $id(item)`. This implies that the DOM elements
126175
* will be associated by item identity in the array.

0 commit comments

Comments
 (0)