Reputation: 410
In a custom filter i have a lot of "if
conditions, i would like to avoid to repeat my code if i have the choice.
I tried to create a function, but it seems don't want to work. Is it possible to create a function inside a filter ? If not, how can i call a function from a filter (if possible) ?
I tried to declare it like this :
$scope.mySearch = function(mySearch, searchArr, filled)
but, got an error message because of $scope.function(mySearch, searchArr, filled)
, but how can i call it ? mySearch : function(mySearch, searchArr, filled)
, got a warning message about the label mySearch.// I TRIED TO INJECT $SCOPE, BUT THE WAY I DID IT WASN'T PROBABLY GOOD :
.filter('searchFilter', function($filter, $scope)
.filter('searchFilter', function($filter)
{
return function(items, mySearch, searchArr)
{
if((mySearch === "") && (searchArr.name !== ''))
{
var firstSearch = $filter('filter')(items, searchArr.name);
var city = $scope.myTestFunc(mySearch.result, searchArr, firstSearch);
return city;
}
// THE FUNCTION I WOULD LIKE TO CALL ABOVE
$scope.myTestFunc = function(mySearch, searchArr, firstSearch)
{
// Do the job
}
};
});
<ion-item ng-repeat="result in results | searchFilter : mySearch.result : searchArr" class="item-avatar">
Upvotes: 1
Views: 55
Reputation: 15711
var my_search = function(mySearch, searchArr, filled){};
//or
function my_search(mySearch, searchArr, filled){}
I'd prefer the first... Store an anonymous function into the variable.
Upvotes: 1