javascript Angular, limitTo 和 $index 跟踪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30201046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:53:15  来源:igfitidea点击:

Angular, limitTo and track by $index

javascriptangularjs

提问by ajmajmajma

I am trying to limit an object coming in by filtering (because there will be an option to show all eventually), however I am running into issues when trying to limitTo and track by index. Here is the code :

我正在尝试通过过滤来限制进入的对象(因为最终会有一个显示所有对象的选项),但是在尝试 limitTo 和按索引跟踪时遇到了问题。这是代码:

 <div ng-repeat="item in filter.values track by $index | limitTo:filterLimit" class="cengage-builder-result-filter-value" value="item" update-filter="updateFilter">

In the controller:

在控制器中:

  $scope.filterLimit = 5;

It's saying I have dupes in the angular error so I'm thinking the track by $index isn't working here. Can't seem to find a proper way to do this, could use some help. Thanks!

据说我在角度错误中被骗了,所以我认为 $index 的轨道在这里不起作用。似乎找不到合适的方法来做到这一点,可以使用一些帮助。谢谢!

回答by New Dev

Filters, like limitTo, orderBy, etc... must come before track by, since they apply to the array source, rather than to the track byexpression.

过滤器,如limitToorderBy等... 必须在 之前track by,因为它们适用于数组源,而不是track by表达式。

<div ng-repeat="item in filter.values | limitTo:filterLimit track by $index">

回答by Harutyun Abgaryan

Try this Use limitTobefore track by

试试这个 使用limitTo之前track by

 <div ng-repeat="item in filter.values | limitTo:filterLimit track by $index" class="cengage-builder-result-filter-value" value="item" update-filter="updateFilter">