I am trying to use a custom filter function with ng-repeat since my filter value is a complex object. I am using angularJS v1.5 and following the docs here: https://code.angularjs.org/1.5.0/docs/api/ng/filter/filter
The docs say you can specify an expression like this: {{ filter_expression | filter : expression : comparator}} And that the comparator function is called with two arguments, the actual object from the array and the predicate value. My comparator function always has 'undefined' for the actual object. I tried making everything as simple as possible and it still sends in undefined for the actual object.
$scope.athletes = [
{
"name":"first",
"tags":[
"Offense"]
},
{
"name":"two",
"tags":[
"Defense"]
},
{
"name":"three",
"tags":[
"Special Teams"]
},
{
"name":"four",
"tags":[
"Offense"]
}
];
$scope.athletesInFunction = [];
$scope.doesAthleteHaveTag = function(athlete,filterValue){
if (athlete) {
$scope.athletesInFunction.push(athlete);
if (athlete.tags.indexOf(filterValue) > -1) {
return true;
}
}
return false;
};
HTML File:
<div ng-repeat="athlete in athletes | filter:filterValue:doesAthleteHaveTag">
<div>Name: {{athlete.name}}</div>
</div>
Example plunkr here: https://plnkr.co/edit/lk26tFFgux2sLpuke56m?p=preview
What am I doing wrong? How do I get it to send in the actual array objects to filter?
EDIT: I should have been clearer with my question. My question is whether or not the docs are valid, and what the recommended way to use a custom filter function is. I currently just use an expression function instead, since the filter predicate is a scope wide variable and I can access it easily in my expression function. I don't know if that is better, worse, or the same as using a comparator function, or if it is better to just write a custom filter as mentioned in one of the answers.