2016-03-17 18 views
0

我試着在這個例子中如何用一個數組

http://www.w3schools.com/angular/tryit.asp?filename=try_ng_filters_filter

<ul> 
    <li ng-repeat="x in names | filter : 'i'"> 
    {{ x }} 
    </li> 
</ul> 

一些angularJS但是如何篩選多種選擇過濾器?在我的情況下,我有一個模型與所有的汽車。用戶從樹列表中選擇ID int[] SelectedCars的列表。

所以我想是這樣

<ul> 
    <li ng-repeat="x in names | filter : SelectedCars[]"> 
    {{ x }} 
    </li> 
</ul> 
+0

我想你可能必須編寫自己的自定義過濾器才能做到這一點。看看[這個答案](http://stackoverflow.com/questions/22024631/angularjs-filter-based-on-array-of-strings)。 – Lex

+0

@Lex是的,那個答案正是我正在尋找的,甚至有一個jsFiddle。謝謝。 –

+0

你可以顯示你的名字json和你必須過濾哪些字段嗎? –

回答

1

JSFiddle

這應該現在的工作。

<!DOCTYPE html> 
<html> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<body> 

<div ng-app="myApp" ng-controller="namesCtrl"> 

<ul> 
    <li ng-repeat="x in names | filter : 'i'"> 
     {{ x }} 
    </li> 
</ul> 

</div> 

<script> 
angular.module('myApp', []).controller('namesCtrl', function($scope) { 
     $scope.names = [ 
       'Jani', 
       'Carl', 
       'Margareth', 
       'Hege', 
       'Joe', 
       'Gustav', 
       'Birgit', 
       'Mary', 
       'Kai' 
     ]; 
     $scope.selectedCars = [1, 2, 5]; 
     $scope.carsToDisplay = []; 
     for (var i = 0; i < $scope.selectedCars.length; i++) { 
       $scope.carsToDisplay.push($scope.names[$scope.selectedCars[i]]); 
     } 
     var p = document.getElementById('p'); 
     p.innerHTML = $scope.carsToDisplay; 
}); 
</script> 

<p id="p">This example displays only the names containing the letter "i".</p> 

</body> 
</html> 
+0

我明白了,但在你的小提琴中,你打印出三個名字,而不是'1,2,5' –

+0

真的,...我就是這樣。 –