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

Tar*_*gar 1 javascript angularjs angularjs-ng-repeat angular-filters

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

$scope.month = 'June'; //this will be a runtime assignment
Run Code Online (Sandbox Code Playgroud)

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

$scope.dates = ['12 June, 2015', '12 April, 2015', '13 May, 2015' ];
Run Code Online (Sandbox Code Playgroud)

这是标记:

<div ng-repeat = "date in dates">
    {{date}}
</div>
Run Code Online (Sandbox Code Playgroud)

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

Jea*_*eri 8

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

<div ng-repeat="date in dates | filterByMonth:month"></div>
Run Code Online (Sandbox Code Playgroud)

然后你可以在你的过滤器中完成剩下的工作

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

演示