由具有多个值的(OR值)财产Angular.js NG-重复过滤器 [英] Angular.js ng-repeat filter by property having one of multiple values (OR of values)

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

问题描述

是否有可能无需编写自定义过滤器来过滤对象的数组,这样的属性值可以是几个值(OR条件)

这是类似于这样的问题 - <一个href=\"http://stackoverflow.com/questions/14733136/angular-js-ng-repeat-filter-by-single-field\">Angular.js NG-重复:单场过滤器

但不是

 &LT; D​​IV NG重复=产品在产品|过滤器:{颜色:红色}&GT;

是否有可能做这样的事情。

 &LT; D​​IV NG重复=产品在产品|过滤器:{颜色:'红'||'蓝'}&GT;

有关样本数据如下: -

  $ scope.products = [
   {ID:1,名称:测试,颜色:红色},
   {ID:2,名称:'鲍勃',颜色:'蓝'}
   / * ...等... * /
];

我已经尝试过失败

 &LT; D​​IV NG重复=产品在产品|过滤器:{颜色:('红'||'蓝')}&GT;


解决方案

要做到这一点的最佳方法是使用一个函数:

 &LT; D​​IV NG重复=产品在产品|过滤器:myfilter的&GT;$ 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.

这篇关于由具有多个值的(OR值)财产Angular.js NG-重复过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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