gpt4 book ai didi

AngularJS : ng-repeat filter when value is greater than

转载 作者:行者123 更新时间:2023-12-02 21:49:08 25 4
gpt4 key购买 nike

我有一个简单的 ng-repeat 会抛出数据,它显示的字段之一是 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 的记录?我试过:

<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

最佳答案

在相关范围上创建谓词函数:

$scope.greaterThan = function(prop, val){
return function(item){
return item[prop] > val;
}
}

作为第一个参数,它采用对象的属性名称。第二个参数是一个整数值。

在您的 View 中像这样使用它:

<tr ng-repeat-start="list in Data.Items | filter: greaterThan('NumberOfStamps', 0)">

Demo

关于AngularJS : ng-repeat filter when value is greater than,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24081004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com