本文介绍了使用“track by"过滤 Angular 1.2 ng-repeat通过布尔属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据布尔属性的值过滤一些列表项,但无论我做什么,始终会显示整个列表.我尝试过的一些东西已经损坏到没有任何显示,但这既不存在也不存在.我无法按需要进行过滤:

I'm trying to filter some list items based on the value of a boolean property, but no matter what I do the entire list is always displayed. A few of the the things I've tried have been so broken that nothing displays, but that's neither here nor there. I can't get my filtering working as desired:

$scope.attendees = [
     {"firstname":"Steve",    "lastname":"Jobs",  "arrived":true,  "id":1}
    ,{"firstname":"Michelle", "lastname":"Jobs",  "arrived":false, "id":2}
    ,{"firstname":"Adam",     "lastname":"Smith", "arrived":true,  "id":3}
    ,{"firstname":"Megan",    "lastname":"Smith", "arrived":false, "id":4}
    ,{"firstname":"Dylan",    "lastname":"Smith", "arrived":false, "id":5}
    ,{"firstname":"Ethan",    "lastname":"Smith", "arrived":false, "id":6}
];

使用以下 ng-repeat 过滤:

Using the following ng-repeat filtering:

<ul>
    <li ng-repeat="person in attendees track by person.id | filter:arrived:'false'">
            {{person.lastname}}, {{person.firstname}}
    </li>
</ul>

我觉得我已经尝试了我能找到的所有引用的排列,其中大部分来自各种 StackOverflow 搜索结果:

I feel like I've tried every permutation that I can find referenced, most of which came from various StackOverflow search results:

  • filter:'arrived'
  • 过滤器:到达
  • filter:'person.arrived'
  • filter:person.arrived
  • filter:{arrived:true}
  • filter:{arrived:'true'}
  • filter:{person.arrived:true}
  • filter:{person.arrived:'true'}

我也尝试过创建自定义过滤器功能:

I've also tried creating a custom filter function:

$scope.isArrived = function(item) {
    return item.arrived;
};

并以此应用它:

  • filter:isArrived
  • filter:'isArrived'
  • filter:{isArrived(person)}
  • filter:isArrived(person)
  • filter:'isArrived(person)'

这些似乎都不起作用.我错过了什么?

None of these seems to work. What am I missing?

这是一个演示我的问题的 plnkr.

推荐答案

track by 需要在表达式的末尾:

The track by needs to be at the end of the expression:

<li ng-repeat="person in attendees | filter: {arrived: false } track by person.id">

这篇关于使用“track by"过滤 Angular 1.2 ng-repeat通过布尔属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:25