简体   繁体   English

何时不在 AngularJS ng-repeat 中使用“按 $index 跟踪”?

[英]When not to use 'track by $index' in an AngularJS ng-repeat?

I recently got the console error `我最近收到控制台错误`

Error: [ngRepeat:dupes] duplicates in a repeater are not allowed.错误:[ngRepeat:dupes] 不允许在转发器中重复。 Use 'track by' expression to specify unique keys...使用“track by”表达式来指定唯一键...

— AngularJS Error Reference - ngRepeat:dupes — AngularJS 错误参考 - ngRepeat:dupes

which I then used 'track by $index' and the issue was solved...然后我使用了'track by $index' ,问题就解决了......

But that got me thinking... is there a reason why you wouldn't want to use track by $index in an ng-repeat?但这让我开始思考……您是否有理由不想在 ng-repeat 中使用track by $index

I've read SO questions like this one as well as other articles , but it seems like almost all of the articles only talk about the advantages of using 'track by' .我已经阅读过类似这样的问题以及其他文章,但似乎几乎所有文章都只讨论了使用'track by'的优点。

Can someone list the disadvantages and give an example of when you wouldn't want to use track by $index ?有人可以列出缺点并举例说明何时不想使用track by $index吗?

It has a disadvantage ,它有一个disadvantage

'track by' expression tracks by index in the array. 'track by'表达式按数组中的索引进行跟踪。 It means that as long as the index stays the same, angularjs thinks it's the same object.这意味着只要索引保持不变,angularjs 就会认为它是同一个对象。

So if you replace any object in the array, angularjs think it didn't change because the index in the array is still the same.所以如果你替换数组中的任何对象,angularjs 认为它​​没有改变,因为数组中的索引仍然相同。 Because of that the change detection wont trigger when you expect it would.因此,更改检测不会在您期望的时候触发。

Take a look at this example看看这个example

Try to change the name, nothing happens.尝试更改名称,没有任何反应。 Remove track by index, it works.按索引删除轨道,它有效。 add track by item.name, it still works.按 item.name 添加轨道,它仍然有效。

There are multiple reasons to avoid track by $index避免track by $index原因有很多

  • Avoid using track by $index when using one-time bindings使用一次性绑定时避免使用track by $index
  • Avoid track by $index when there is a unique property identifier当存在唯一的属性标识符时,避免track by $index
  • Other Examples of problems with track by $index track by $index问题的其他示例

Avoid using track by $index when using one-time bindings使用一次性绑定时避免使用track by $index

The documentation specifically states that track by $index should be avoided when using one-time bindings.该文档特别指出在使用一次性绑定时应避免track by $index进行track by $index

From the Docs:从文档:

Avoid using track by $index when the repeated template contains one-time bindings .当重复模板包含一次性绑定时,避免使用track by $index 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.在这种情况下,第nth DOM 元素将始终与数组的nth项目匹配,因此即使相应项目发生更改,该元素上的绑定也不会更新,从而导致视图与基础数据。

— AngularJS ng-repeat Reference - Tracking and Duplicates — AngularJS ng-repeat 参考 - 跟踪和重复


Avoid track by $index when there is a unique property identifier当存在唯一的属性标识符时,避免track by $index

Avoid track by $index when there is a unique property identifier to work with.当有一个唯一的属性标识符可以使用时,避免track by $index When working with objects that are all unique , it is better to let ng-repeat to use its own tracking instead of overriding with track by $index .当处理所有唯一的对象时,最好ng-repeat使用它自己的跟踪,而不是track by $index覆盖track by $index

From the Docs:从文档:

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.如果您稍后重新加载数据, ngRepeat将不必为它已经呈现的项目重建 DOM 元素,即使集合中的 JavaScript 对象已被新对象替换。 For large collections, this significantly improves rendering performance.对于大型集合,这显着提高了渲染性能。

— AngularJS ng-repeat Directive API Reference - Tracking — AngularJS ng-repeat 指令 API 参考 - 跟踪


Other Examples of problems with track by $index track by $index问题的其他示例

I have also seen problems when objects are added and removed from arrays of objects.在对象数组中添加和删除对象时,我也遇到过问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM