一尘不染

通过布尔属性用“ track by”过滤Angular 1.2 ng-repeat

angularjs

我正在尝试根据布尔属性的值过滤一些列表项,但是无论我做什么,总会显示整个列表。我尝试过的一些东西已经坏掉了,什么也没显示,但这既不存在也不存在。我无法按需进行过滤:

$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过滤:

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

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

  • filter:'arrived'
  • filter:arrived
  • filter:'person.arrived'
  • filter:person.arrived
  • filter:{arrived:true}
  • filter:{arrived:'true'}
  • filter:{person.arrived:true}
  • filter:{person.arrived:'true'}

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

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

并因此应用它:

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

这些似乎都不起作用。我想念什么?

这是演示我问题的plnkr


阅读 225

收藏
2020-07-04

共1个答案

一尘不染

track by必须位于表达式的末尾:

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