在自定义过滤器中,我有很多"if条件,如果可以选择,我想避免重复我的代码。

我试图创建一个函数,但似乎不想工作。是否可以在过滤器内部创​​建函数?如果没有,如何从过滤器中调用函数(如果可能)?

我试图这样声明:


$scope.mySearch = function(mySearch, searchArr, filled)但是,由于$ scope导致出现错误消息。
function(mySearch, searchArr, filled),但是我怎么称呼它?
mySearch : function(mySearch, searchArr, filled),收到有关标签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">

最佳答案

var my_search = function(mySearch, searchArr, filled){};
//or
function my_search(mySearch, searchArr, filled){}


我希望第一个...将匿名函数存储到变量中。

10-05 22:28