ng-repeat 中的 angularJS 过滤器表达式 [英] angularJS filter expression in ng-repeat

查看:28
本文介绍了ng-repeat 中的 angularJS 过滤器表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道实现这一点的最优雅、最简单的方法是什么.我需要为 ng-repeat 添加一个过滤器表达式,以便从一个属性中过滤 2 个条件.

I would like to know what is the most elegant and simple way to implement this. I need to add a filter expression for a ng-repeat that would filter 2 conditions from one property.

在这个例子中 http://plnkr.co/edit/OMQxXvSjtuudMRGE4eZ8?p=preview

  • 如果你输入A,它会显示A的复选框,
  • 输入 B - 显示 B 的复选框.

但我想显示指定的复选框以及任何条件为空的内容.C没有条件,所以:

But I want to display the specified checkboxes plus anything with empty condition. There is no condition for C, so:

  • 如果你输入A,我想同时显示A和C复选框,
  • 输入 B,我想同时显示 B 和 C 复选框.

推荐答案

我会创建自定义过滤器,例如:

I would create custom filter like:

app.filter('myfilter', function() {
   return function( items, condition) {
    var filtered = [];

    if(condition === undefined || condition === ''){
      return items;
    }

    angular.forEach(items, function(item) {          
       if(condition === item.condition ||  item.condition === ''){
         filtered.push(item);
       }
    });

    return filtered;
  };
});

和用法:

<span ng-repeat="charge in charges  |  myfilter:level.condition">

查看 Plunker

它看起来非常优雅和清晰.

It looks pretty elegant and clear.

希望能帮到你

这篇关于ng-repeat 中的 angularJS 过滤器表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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