ng-repeat track by 不工作:缓慢且仍在生成 $$hashKey

ng-repeat track by is not working: slow and still generating $$hashKey

我有一个 ng-repeat 设置如下:

ng-repeat="article in main[main.mode].primary | orderBy: main[main.mode].primary.filter.order
track by article.url"

main[main.mode].primary 是一个数组,....filter.order 是一个字符串。

根据this blog post

Behind the scenes ngRepeat adds a $$hashKey property to each task to keep track of it. If you replace the original tasks with new tasks objects from the server, even if those are in fact totally identical to your original tasks, they won’t have the $$hashKey property and so ngRepeat won’t know they represent the same elements.

重新生成列表是一项非常常见的任务,应用程序会挂起超过一秒钟,因此我对 track by 感兴趣。根据我看过的许多问题和文档,我使用了正确的语法来对数组进行排序和跟踪。来自 the docs

item in items | filter:searchText track by item.id is a pattern that might be used to apply a filter to items in conjunction with a tracking expression.

为什么跟踪没有被实施?我是 运行 angular 1.3.11.

编辑 如果我删除 orderBy 参数

它甚至不起作用
ng-repeat="article in main[main.mode].primary track by article.url"

根据 Angular 文档 orderBy 仅适用于数组,因此如果您迭代一个对象,您将无法使用它,除非您将对象转换为数组

https://docs.angularjs.org/api/ng/filter/orderBy

还有其他方法可以处理此问题,要么实现您自己的过滤器,要么将您的对象转换为具有键值属性的对象数组。像

var narr=[]
angular.forEach(object,function(k,v){
  narr.push({key:v,value:v})
})

现在 narr 是一个数组,可以使用 orderBy

按键或值排序