gpt4 book ai didi

javascript - Angularjs:过滤包含子字符串的ng-repeat中的项目

转载 作者:行者123 更新时间:2023-11-30 07:14:42 25 4
gpt4 key购买 nike

所以我有一个动态设置的范围变量:

$scope.month = 'June'; //this will be a runtime assignment

我有一个数组(示例),我必须使用 ng-repeat 对其进行迭代:

$scope.dates = ['12 June, 2015', '12 April, 2015', '13 May, 2015' ];

这是标记:

<div ng-repeat = "date in dates">
{{date}}
</div>

现在我想要实现的是,在 ng-repeat 循环中,我只打印那些包含存储在 $scope.month 中的月份的日期。我如何使用过滤器执行此操作?

最佳答案

您可以将参数传递给过滤器

<div ng-repeat="date in dates | filterByMonth:month"></div>

然后你可以在你的过滤器中解决剩下的问题

myApp.filter("filterByMonth", function() { 
return function (dates, month) {
return dates.filter(function (item) {
return item.indexOf(month) > -1;
});
};
});

demo

关于javascript - Angularjs:过滤包含子字符串的ng-repeat中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350017/

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