gpt4 book ai didi

javascript - AngularJS ng-repeat 忽略过滤函数

转载 作者:行者123 更新时间:2023-11-30 16:56:47 25 4
gpt4 key购买 nike

我想使用带有 ng-repeat 的过滤器。不幸的是,它似乎忽略了附加到过滤器的函数的结果。

HTML:

<a ng-repeat="item in date track by $index | filter: filterFunction('2015-09-23',item)" class="item item-icon-left" >
<div class="listcontent">{{title[$index]}}
<br> <span class="subinfo">{{item}}</span>
</div>
<i class="iconarrowmore ion-chevron-right"></i>
</a>

JS:

$scope.filterFunction = function (datestamp, element) {
if (datestamp == element) {
console.log(datestamp == element);
return true;
} else {
console.log(datestamp == element);
return false;
}
};

当我使用 console.log 调试它时,它返回 true 或 false,但每个项目仍然出现在列表中。

enter image description here

我真的不知道它为什么这样做。

最佳答案

您的过滤函数未定义。请注意,使用 filter: filterFunction('2015-09-23',item) 您会立即执行 filterFunction 并将其结果用作过滤器,即 undefined 因为你的函数不返回任何东西。

要修复它,请使 filterFunction 返回新的过滤器函数:

$scope.filterFunction = function(datestamp) {
return function(element) {
return datestamp == element;
};
};

然后 HTML 部分将是相同的,只是您不需要传递项目:

ng-repeat="item in date track by $index | filter: filterFunction('2015-09-23')"

看到它工作:http://plnkr.co/edit/jLQbgGcLSAxwmiCcYZdP?p=preview

关于javascript - AngularJS ng-repeat 忽略过滤函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29589649/

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