本文介绍了AngularJS:当值大于时 ng-repeat 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的 ng-repeat 会抛出数据,它显示的字段之一是 NumberOfStamps:
I have a simple ng-repeat that throws out data, one of fields it displays is NumberOfStamps:
<tr ng-repeat-start="list in Data.Items ">
<td><a href=" {[{list.Title}]} {[{list.ForeName}]} {[{list.SurName}]}</a></td>
<td>(Date of Birth {[{list.Dob}]})</td>
<td>{[{list.NumberOfStamps}]} stamps</td>
</tr>
示例输出:
Mr Adam Happy Date of Birth 01/6/1984 16 stamps
Mr Adam Sad Date of Birth 24/11/1975 0 stamps
Mr Adam Green Date of Birth 02/1/1963 1 stamps
Mr Adam Red Date of Birth 21/1/1951 12 stamps
Mr Adam Blue Date of Birth 28/10/1998 0 stamps
Mr Adam Brown Date of Birth 25/9/2010 0 stamps
Mr Adam Black Date of Birth 24/8/1954 21 stamps
Mr Adam Violet Date of Birth 17/5/1942 54 stamps
如何修改此 ng-repeat 以仅显示 NumberOfStams > 0 的记录?我试过了:
How can i modify this ng-repeat to only show records where the NumberOfStams is > 0? I've tried:
<tr ng-repeat-start="list in Data.Items | filter:{NumberOfStamps > 0}">
<td><a href=" {[{list.Title}]} {[{list.ForeName}]} {[{list.SurName}]}</a></td>
<td>(Date of Birth {[{list.Dob}]})</td>
<td>{[{list.NumberOfStamps}]} stamps</td>
</tr>
预期输出:
Mr Adam Happy Date of Birth 01/6/1984 16 stamps
Mr Adam Green Date of Birth 02/1/1963 1 stamps
Mr Adam Red Date of Birth 21/1/1951 12 stamps
Mr Adam Black Date of Birth 24/8/1954 21 stamps
Mr Adam Violet Date of Birth 17/5/1942 54 stamps
推荐答案
在相关作用域上创建谓词函数:
Create a predicate function on the relevant scope:
$scope.greaterThan = function(prop, val){
return function(item){
return item[prop] > val;
}
}
作为第一个参数,它采用对象的属性名称.第二个参数是一个整数值.
As a first argument, it takes a property name on the object. The second argument is an integer value.
像这样在您的视图中使用它:
Use it in your view like this:
<tr ng-repeat-start="list in Data.Items | filter: greaterThan('NumberOfStamps', 0)">
这篇关于AngularJS:当值大于时 ng-repeat 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!