如何使用一个数组进行过滤
How filter using one array
我正在尝试 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>
但是您如何过滤多个选择?就我而言,我有一个包含所有汽车的模型。并且用户 select 来自树列表的 ID int[] SelectedCars
列表。
所以我想要类似
的东西
<ul>
<li ng-repeat="x in names | filter : SelectedCars[]">
{{ x }}
</li>
</ul>
现在应该可以了。
<!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>
我正在尝试 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>
但是您如何过滤多个选择?就我而言,我有一个包含所有汽车的模型。并且用户 select 来自树列表的 ID int[] SelectedCars
列表。
所以我想要类似
的东西<ul>
<li ng-repeat="x in names | filter : SelectedCars[]">
{{ x }}
</li>
</ul>
现在应该可以了。
<!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>