简体   繁体   English

当在ng-repeat中使用过滤器通过$ index跟踪时,AngularJs错误

[英]AngularJs error when usin track by $index with filter in a ng-repeat

I'm having trouble when using a filter on a ng-repeat list and track by $index . 我在ng-repeat列表上使用过滤器并按track by $index时遇到麻烦。 I have a list of hotels and need to filter it by the city name. 我有一个旅馆列表,需要按城市名称过滤。

The filter is working fine, this isn't the problem. 过滤器工作正常,这不是问题。 The problem is the rendered list AFTER the filter. 问题是过滤器之后的呈现列表。 There is 2 situations at the moment: 目前有2种情况:

  • 1: If not using track by or using track by item.id there is no error at the beggining, but if I select an option I'll get the error: [ngRepeat:dupes] , which can be solved by using track by $index . 1:如果不使用track by或使用track by item.id ,则开始时没有错误,但是如果选择一个选项,则会出现错误: [ngRepeat:dupes] ,可以通过使用track by $index来解决track by $index

  • 2: If using track by $index there is no errors, no matter what option I select. 2:如果使用track by $index进行track by $index则无论选择什么选项都没有错误。 BUT, the list doesn't update properly. 但是,该列表未正确更新。 When I select one city the list will show the amount of results, but not the correct results. 当我选择一个城市时,列表将显示结果数量,但不会显示正确的结果。 For example, if there is a total of 12 hotels and after the select there is only 4 results. 例如,如果总共有12家酒店,则选择之后只有4个结果。 The list will have 4 results, but it's not updating the results, it only reduce the list to 4 results but keeps the same order. 该列表将有4个结果,但它不会更新结果,只会将列表缩小为4个结果,但顺序相同。

See this plunkr for example: https://plnkr.co/edit/J1QQCM?p=preview 例如,请参见此plunkr: https ://plnkr.co/edit/J1QQCM?p = preview

If you filter by 'New York', the list will have only 4 results, but doesn't reorder the list to show the results from the city 'New York'. 如果您按“纽约”进行过滤,则该列表将仅包含4个结果,但不会对列表进行重新排序以显示城市“纽约”的结果。 And this only happens when using track by $index . 这仅在使用track by $index时发生。

This is the html I'm using: 这是我正在使用的html:

<li ng-repeat="item in vm.mainList | filter:{location:filterLocation} track by $index">...</li>

And the array is something very basic, like this: 数组是非常基本的东西,像这样:

{
    "id": 0,
    "title": "Hotel The Mirage (Hotel & Casino)",
    "location": "Las Vegas, USA"
},
{
    "id": 1,
    "title": "Wynn Las Vegas",
    "location": "Las Vegas, USA"
},[...]~

Note: I know I could solve this by using track by item.id , but this is a simple example. 注意:我知道我可以通过使用track by item.id来解决此track by item.id ,但这是一个简单的示例。 On the actual list I have more nested array and eventually I'll end up with id repeting. 在实际的列表上,我有更多的嵌套数组,最终我将以id重复结束。 So I need to be able to use track by $index . 因此,我需要能够使用track by $index

It's not working because you're using the one-time binding syntax. 它不起作用,因为您使用的是一次性绑定语法。

I've forked your plunkr and used the regular syntax and it works: https://plnkr.co/edit/mLaiqsrk9uUqnaYe308F?p=preview 我已经分叉了您的plunkr并使用了常规语法,它可以正常工作: https ://plnkr.co/edit/mLaiqsrk9uUqnaYe308F?p=preview

  <ul>
    <li ng-repeat="item in vm.mainList | filter:{location:filterLocation} track by $index">
        <h4>{{ item.title }} - {{$index}}</h4>
        <p>{{ item.location }}</p>
    </li>
  </ul>

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

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