ng-repeat中的angularJS过滤器表达式 [英] angularJS filter expression in ng-repeat
本文介绍了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屋!
查看全文