2016-09-20 268 views
-1

我有checkboxs的列表:角度濾波器

<input type="checkbox" ng-model="myFilter.filterField" ng-true-value="'value1'" ng-false-value="''"> value1<br> 
<input type="checkbox" ng-model="myFilter.filterField" ng-true-value="'value2'" ng-false-value="''"> value2<br> 

.... .... 我要篩選這樣的選中的複選框字段:

x in X | filter: { 'filterField': ['value1','value2']} 

任何解決方案推入我的陣列filterField並篩選多個值?謝謝!

+0

你有沒有考慮創建自己的過濾器? – taguenizy

+0

我將需要更多的filterFields,可能這個filterFields也將數組爲多個值,我該怎麼做? – Charly

+0

這可能有助於http://stackoverflow.com/questions/16563018/angularjs-custom-filters-and-ng-repeat – taguenizy

回答

0

這是更好地使用自定義過濾器在這種情況下

$scope.filterField = function(x) { 
    return x.filterField == 'value1' || x.filterField == 'value2' || x.anotherField == 'value'; 
}; 


x in X | filter: filterField 
+0

但如果我想要更多的過濾器字段,我需要爲每個字段創建一個函數? – Charly

+0

你可以像這樣使用x.filterField =='value1'|| x.field1 =='value'|| x.field2 =='value1'@Charly –

0

AngularJS : Custom filters and ng-repeat

檢查多個字段

// Filter, where element would be your x 
$scope.filterFn = function(element) 
{ 
    if(element.field === 'value1' || element.anotherField === 'value2') 
    { 
     return true; // this will be listed in the results 
    } 
    return false; // otherwise it won't be within the results 
}; 

,然後應用它
x in X | filter:filterFn

0

另一種方法是使用一個自定義過濾器並向其中添加參數。這小提琴展示瞭如何使用過濾器的工作與多個參數: http://jsfiddle.net/halirgb/Lvc0u55v/

像這樣

myApp.filter('customFilter', function() { // Name of filter 
return function(first, second,third) { 
// write filter code here 
    return second;// shows the last filter parameter 
};}); 

而且使用這樣的過濾器:

<div ng-repeat="x in X"> {{x|customFilter:myFilter.filterField}} </div>