From 3e87f54922a2a14bebf4fc4e63a6fb39e5054b75 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Mon, 7 Nov 2016 12:50:27 +0200 Subject: [PATCH] docs(ngRepeat): add warning about `track by $index` with one-time bindings --- src/ng/directive/ngRepeat.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/ng/directive/ngRepeat.js b/src/ng/directive/ngRepeat.js index 0b1240d5c..127d9c247 100644 --- a/src/ng/directive/ngRepeat.js +++ b/src/ng/directive/ngRepeat.js @@ -62,7 +62,7 @@ * # Tracking and Duplicates * * `ngRepeat` uses {@link $rootScope.Scope#$watchCollection $watchCollection} to detect changes in - * the collection. When a change happens, ngRepeat then makes the corresponding changes to the DOM: + * the collection. When a change happens, `ngRepeat` then makes the corresponding changes to the DOM: * * * When an item is added, a new instance of the template is added to the DOM. * * When an item is removed, its template instance is removed from the DOM. @@ -70,7 +70,7 @@ * * To minimize creation of DOM elements, `ngRepeat` uses a function * to "keep track" of all items in the collection and their corresponding DOM elements. - * For example, if an item is added to the collection, ngRepeat will know that all other items + * For example, if an item is added to the collection, `ngRepeat` will know that all other items * already have DOM elements, and will not re-render them. * * The default tracking function (which tracks items by their identity) does not allow @@ -97,19 +97,29 @@ * ``` * *
- * If you are working with objects that have an identifier property, you should track - * by the identifier instead of the whole object. Should you reload your data later, `ngRepeat` + * If you are working with objects that have a unique identifier property, you should track + * by this identifier instead of the object instance. Should you reload your data later, `ngRepeat` * will not have to rebuild the DOM elements for items it has already rendered, even if the * JavaScript objects in the collection have been substituted for new ones. For large collections, * this significantly improves rendering performance. If you don't have a unique identifier, * `track by $index` can also provide a performance boost. *
+ * * ```html *
* {{model.name}} *
* ``` * + *
+ *
+ * Avoid using `track by $index` when the repeated template contains + * {@link guide/expression#one-time-binding one-time bindings}. In such cases, the `nth` DOM + * element will always be matched with the `nth` item of the array, so the bindings on that element + * will not be updated even when the corresponding item changes, essentially causing the view to get + * out-of-sync with the underlying data. + *
+ * * When no `track by` expression is provided, it is equivalent to tracking by the built-in * `$id` function, which tracks items by their identity: * ```html @@ -118,15 +128,17 @@ * * ``` * + *
*
* **Note:** `track by` must always be the last expression: *
* ``` - *
- * {{model.name}} - *
+ *
+ * {{model.name}} + *
* ``` * + * * # Special repeat start and end points * To repeat a series of elements instead of just one parent element, ngRepeat (as well as other ng directives) supports extending * the range of the repeater by defining explicit start and end points by using **ng-repeat-start** and **ng-repeat-end** respectively.