|
48 | 48 | * or implement a `$watch` on the object yourself.
|
49 | 49 | *
|
50 | 50 | *
|
| 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 | + * |
51 | 100 | * # Special repeat start and end points
|
52 | 101 | * To repeat a series of elements instead of just one parent element, ngRepeat (as well as other ng directives) supports extending
|
53 | 102 | * the range of the repeater by defining explicit start and end points by using **ng-repeat-start** and **ng-repeat-end** respectively.
|
|
115 | 164 | *
|
116 | 165 | * For example: `(name, age) in {'adam':10, 'amalie':12}`.
|
117 | 166 | *
|
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. |
124 | 173 | *
|
125 | 174 | * For example: `item in items` is equivalent to `item in items track by $id(item)`. This implies that the DOM elements
|
126 | 175 | * will be associated by item identity in the array.
|
|
0 commit comments