簡體   English   中英

使用各種數組或對象項過濾ng-repeat?

[英]filter ng-repeat using various array or object items?

好吧,我有一個ng-repeat:

<div id="scrolltwo" class="animate-repeat panel" ng-repeat="items in arrayfull |filter:filtertype ">

在filtertype中,我有:

[[0] => "Typeone", [1] => "Typetwo" ]

我如何讓過濾器遍歷整個數組並匹配與值匹配的結果?

使用自定義過濾器進行更新:

<div id="scrolltwo" class="animate-repeat panel" ng-repeat="items in arrayfull | array | filter:matchaccnttypes(accnttypes)">

起作用的數組過濾器:

.filter('array', function() {
  return function(items) {
    console.log(items);
    var filtered = [];
    angular.forEach(items, function(item) {
      filtered.push(item);
    });
   return filtered;
  };
})

不匹配的matchaccountypes:

                    $scope.matchaccnttypes = function matchaccnttypes(query) {
                        return function(items) {
                                return items.Organtype.match(query); 
                            angular.forEach($scope.accnttypes, function(value, key){
                                console.log(key + ': ' + value);
                             });
                        }
                    }; 

它只是返回傳入$ scope.accntypes數組的最后一個值,這是我上面的示例數組。

在underscoreJS的幫助下的自定義過濾器。 這是小提琴

app.filter('arrayFilter', function() {
    return function(input, filterType) {
         return  _.intersection(input,filterType);
    };
});

沒有UnderscoreJS

app.filter('arrayFilter', function() {
    return function(input, filterType) {
       var filterdArray =[];
         angular.forEach(input, function(inputValue, key){
             angular.forEach(filterType, function(filterTypeValue, key){
                 if(inputValue==filterTypeValue){
                     filterdArray.push(inputValue);
                 }
     });

     });
     return filterdArray;

    };
});

ng-repeat應該按以下方式使用。

ng-repeat="array in arrayfull | arrayFilter : filterType"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM