所以我有一个动态设置的范围变量:
$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;
});
};
});
关于javascript - Angularjs:过滤包含子字符串的ng-repeat中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350017/