|
62 | 62 | * # Tracking and Duplicates |
63 | 63 | * |
64 | 64 | * `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: |
66 | 66 | * |
67 | 67 | * * When an item is added, a new instance of the template is added to the DOM. |
68 | 68 | * * When an item is removed, its template instance is removed from the DOM. |
69 | 69 | * * When items are reordered, their respective templates are reordered in the DOM. |
70 | 70 | * |
71 | 71 | * To minimize creation of DOM elements, `ngRepeat` uses a function |
72 | 72 | * 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 |
74 | 74 | * already have DOM elements, and will not re-render them. |
75 | 75 | * |
76 | 76 | * The default tracking function (which tracks items by their identity) does not allow |
|
97 | 97 | * ``` |
98 | 98 | * |
99 | 99 | * <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` |
102 | 102 | * will not have to rebuild the DOM elements for items it has already rendered, even if the |
103 | 103 | * JavaScript objects in the collection have been substituted for new ones. For large collections, |
104 | 104 | * this significantly improves rendering performance. If you don't have a unique identifier, |
105 | 105 | * `track by $index` can also provide a performance boost. |
106 | 106 | * </div> |
| 107 | + * |
107 | 108 | * ```html |
108 | 109 | * <div ng-repeat="model in collection track by model.id"> |
109 | 110 | * {{model.name}} |
110 | 111 | * </div> |
111 | 112 | * ``` |
112 | 113 | * |
| 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 | + * |
113 | 123 | * When no `track by` expression is provided, it is equivalent to tracking by the built-in |
114 | 124 | * `$id` function, which tracks items by their identity: |
115 | 125 | * ```html |
|
118 | 128 | * </div> |
119 | 129 | * ``` |
120 | 130 | * |
| 131 | + * <br /> |
121 | 132 | * <div class="alert alert-warning"> |
122 | 133 | * **Note:** `track by` must always be the last expression: |
123 | 134 | * </div> |
124 | 135 | * ``` |
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> |
128 | 139 | * ``` |
129 | 140 | * |
| 141 | + * |
130 | 142 | * # Special repeat start and end points |
131 | 143 | * To repeat a series of elements instead of just one parent element, ngRepeat (as well as other ng directives) supports extending |
132 | 144 | * 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