Angular.js ng-repeat 过滤器按具有多个值之一的属性(值的 OR) [英] Angular.js ng-repeat filter by property having one of multiple values (OR of values)

查看:23
本文介绍了Angular.js ng-repeat 过滤器按具有多个值之一的属性(值的 OR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以过滤一个对象数组,使得属性的值可以是几个值中的一个(OR 条件)而无需编写自定义过滤器

这类似于这个问题——Angular.js ng-repeat:按单个字段过滤

但不是

是否可以做这样的事情

<div ng-repeat="product in products | filter: { color: 'red'||'blue' }">

示例数据如下-

$scope.products = [{ id: 1, name: 'test', color: 'red' },{ id: 2, name: 'bob', color: 'blue' }/*... 等等... */];

我试过没有成功

<div ng-repeat="product in products | filter: { color: ('red'||'blue') }">

解决方案

最好的方法是使用函数:

$scope.myFilter = 函数(项目){返回项目 === '红色' ||项目 === '蓝色';};

或者,您可以使用 ngHide 或 ngShow 根据特定条件动态显示和隐藏元素.

Is it possible to filter an array of objects, such that the value of property can be either of a few values (OR condition) without writing a custom filter

This is similar to this problem - Angular.js ng-repeat :filter by single field

But instead of

<div ng-repeat="product in products | filter: { color: 'red' }">

is it possible to do something like this

<div ng-repeat="product in products | filter: { color: 'red'||'blue' }">

for a sample data as follows-

$scope.products = [
   { id: 1, name: 'test', color: 'red' },
   { id: 2, name: 'bob', color: 'blue' }
   /*... etc... */
];

I've unsuccessfully tried

<div ng-repeat="product in products | filter: { color: ('red'||'blue') }">

解决方案

Best way to do this is to use a function:

<div ng-repeat="product in products | filter: myFilter">

$scope.myFilter = function (item) { 
    return item === 'red' || item === 'blue'; 
};

Alternatively, you can use ngHide or ngShow to dynamically show and hide elements based on a certain criteria.

这篇关于Angular.js ng-repeat 过滤器按具有多个值之一的属性(值的 OR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆