1

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">
2
  • Try () => {} maybe that might work Commented Sep 8, 2017 at 9:58
  • 1
    Would you not just call it as a plain function? var city = myTestFunc(...) and function myTestFunc(mySearch, searchArr, firstSearch) Commented Sep 8, 2017 at 10:03

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd prefer second, which doesn't require the function definition to be on top of the code using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.