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}
];
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>
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:arrivedfilter:'person.arrived'filter:person.arrivedfilter:{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;
};
And applying it thusly:
filter:isArrivedfilter:'isArrived'filter:{isArrived(person)}filter:isArrived(person)filter:'isArrived(person)'
None of these seems to work. What am I missing?
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">
@Gruff's answer is right, but just to give an answer from an official source:
From the Angular
ng-repeatdocs:Note:
track bymust always be the last expression:<div ng-repeat="model in collection | orderBy: 'id' as filtered_result track by model.id"> {{model.name}} </div>
It also appear on the "Arguments" part of the docs:
Note that the tracking expression must come last, after any filters, and the alias expression.
来源:https://stackoverflow.com/questions/21416198/filtering-an-angular-1-2-ng-repeat-with-track-by-by-a-boolean-property