如何过滤使用一个数组

时间:2016-03-17 21:12:46

标签: javascript angularjs angularjs-filter

我在这个例子中尝试了一些angularJS

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

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

但是你如何过滤多种选择?在我的情况下,我有一个模型与所有的汽车。用户从树列表中选择ID int[] SelectedCars列表。

所以我想要像

这样的东西
<ul>
  <li ng-repeat="x in names | filter : SelectedCars[]">
    {{ x }}
  </li>
</ul>

1 个答案:

答案 0 :(得分: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>