[英]AngularJS logical expression in filter?
Basically I want to filter to more than one value in a list. 基本上,我想过滤列表中的多个值。 I have a list of filters called items which represent a value for the listitems in the list below.
我有一个称为项的过滤器列表,这些过滤器表示下面列表中列表项的值。 If the user click on the link One it will list only the listitems that has 1 as a value, clicking on Two list the listitems with a value of 2 etc..
如果用户单击链接“一”,它将仅列出值为1的列表项,单击“二”将列出值为2的列表项,等等。
The main problem comes from a link called OneTwo which should list the listitems which has value of 1 or 2, but I simply cannot make it work. 主要问题来自名为OneTwo的链接,该链接应列出值为1或2的列表项,但我根本无法使其工作。 I tried to use dot to include more than one value, but I just cannot make it work.
我试图使用点来包含多个值,但我只是无法使其起作用。 In the example I set the value of OneTwo to '0', but it should be something like ('1'.'2').
在示例中,我将OneTwo的值设置为'0',但它应该类似于('1'。'2')。 Codes and JSFiddle example below:
下面的代码和JSFiddle示例:
HTML: HTML:
<div ng-controller="FilterListController">
<ul>
<li ng-repeat="item in items">
<a ng-click="select(item)" ng-class="{active: item == selected}">{{item.name}}</a>
</li>
</ul>
<ul>
<li ng-repeat="listitem in list | filter:{value: selected.value}">
{{listitem.name}} - {{listitem.value}}
</li>
</ul>
</div>
JS: JS:
var myApp = angular.module('myApp',[]);
function FilterListController($scope) {
$scope.list = [
{name: 'a', value: '1'},
{name: 'b', value: '2'},
{name: 'c', value: '3'}];
$scope.items = [{name: 'OneTwo', value: '0'},{name: 'One', value: '1'},{name: 'Two', value: '2'},{name: 'Three', value: '3'}];
$scope.selected = $scope.items[0];
$scope.select = function(item){
$scope.selected = item;
}
}
You should use a custom filter from the controller and then display your list, 您应该使用控制器中的自定义过滤器,然后显示列表,
var myApp = angular.module('myApp',[]);
function FilterListController($scope, $filter) {
$scope.list = [
{name: 'a', value: '1'},
{name: 'b', value: '2'},
{name: 'c', value: '3'}];
$scope.items = [{name: 'OneTwo', value: '12'},{name: 'One', value: '1'},{name: 'Two', value: '2'},{name: 'Three', value: '3'}];
$scope.selected = $scope.items[0];
$scope.select = function(item){
$scope.selected = item;
$scope.resultList = $filter('filterMyList')($scope.list, item);
}
}
myApp.filter('filterMyList', function() {
return function(list, val) {
var newList = [];
for (var i in list) {
if(val.value.indexOf(list[i].value) !== -1) {
newList.push(list[i]);
}
}
return newList;
}
})
Updated JSFiddle : http://jsfiddle.net/bikiew/c7jQk/1/ 更新的JSFiddle: http : //jsfiddle.net/bikiew/c7jQk/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.