Memphis
Memphis

Reputation: 410

Is it possible to write a function inside a filter in AngularJS?

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 :

// 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

Answers (1)

Salketer
Salketer

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

Related Questions