In my AngularJS web application, I have created a custom filter to search through posts:
app.filter('myfilter', function() {
return function(postList, term) {
var out = [];
if(!postList) return out;
if(!term) return postList;
var i;
for(i = 0; i < postList.length; i++){
if(postList[i].title.indexOf(term) >=0){
out.push(postList[i]);
}
if($scope.isContentFilterOn.checked){
if(postList[i].text.indexOf(term) >=0){
out.push(postList[i]);
}
}
}
}
return out;
}
});
The problem with the above code is that it cannot access scope variables directly. Simply passing $scope
as an argument will not work either. Is there a more efficient way to achieve this?
Edit: source http://plnkr.co/edit/G9BFBTaKdUmki8MJegrn?p=catalogue