I have developed an application where the content filter changes depending on which button is clicked. I have managed to figure out the straightforward filters, but now I am attempting to create a filter that displays content within a specified range.
Below is the function I am using:
$scope.isGood = function(item){
if(!isNan(item.rating)){
return (item.rating>49);
}
return alert("Something broke!!");
//returns true if the item has a high rating (is good, 50+)
//returns false if the item has a poor rating (is bad, 49-)
}
As the filter changes based on the navigation button clicked, and the navigation is dynamic, for each navigation button click, I execute a function that retrieves the filter from its link.filter
section and assigns it to $scope.selectedFilter
, a variable used in my ng-repeat
like this:
<tr class="progress" ng-repeat="idea in ideas | filter:selectedFilter">
Considering I have multiple filters, instead of injecting my own $filter and needing to chain them together, I intend to write something that can fit into the $scope.selectedFilter
variable since only one filter should be active at a time.
Here is an example of how the dynamic navigation is structured:
$scope.links = [
{'name':'About',
'URL': '#/about',
'filter':''
},
{'name':'Active',
'URL': '#/active',
'filter': {active: true}
},
{'name':'Inactive',
'URL': '#/active',
'filter':{active: false}
},
{'name':'Good Rating',
'URL': '#/active',
'filter': 'isGood(item.rating)'
},
{'name':'Bad Rating',
'URL': '#/active',
'filter':'!isGood(item.rating)'
}
];
Below is the content I would like to filter:
$scope.ideas = [
{'title':'Title A',
'rating': 80,
active: true
},
{'title':'Title B',
'rating': 55,
active: false
},
{'title':'Title C',
'rating': 10,
active: true
}
];
Despite my efforts, I am unable to get the filter to work, and I am uncertain about where I might have made a mistake. As I am relatively new to Angular, could there be an error in my syntax somewhere? I tried following the structure ofthis answer, but no success.
I would greatly appreciate any assistance. Thank you!