我正在尝试根据布尔属性的值过滤一些列表项,但无论我做什么,总是会显示整个列表。我尝试过的一些东西已经破碎,没有任何东西显示出来,但这既不在这里,也不在那里。我无法按照需要进行过滤:
$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)'
这些似乎都不起作用。我错过了什么?
答案 0 :(得分:243)
轨道需要位于表达式的末尾:
<li ng-repeat="person in attendees | filter: {arrived: false } track by person.id">
答案 1 :(得分:2)
@Gruff's answer是对的,但只是从官方来源给出答案:
来自Angular
ng-repeat
docs:注意:
track by
必须始终是最后一个表达:<div ng-repeat="model in collection | orderBy: 'id' as filtered_result track by model.id"> {{model.name}} </div>
它也出现在文档的“参数”部分:
请注意,在任何过滤器之后,跟踪表达式必须位于最后 和别名表达式。