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

标签 javascript angularjs angularjs-ng-repeat angular-filters

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

$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/

相关文章:

javascript - React app not Building 显示代码 ELIFECYCLE

javascript - 如何解析对象的实例并与数组的内容进行比较?

javascript - Angularjs 在 ng-repeat 中找到最后一个可见元素

angularjs ng 重复表,得到高度错误

javascript - "error:ReferenceError: Security error: attempted to read protected variable: write"与 Opera 11.5 和 window.open 和 document.write

javascript - meteor js : Create text index in collection

javascript - 使用 ocLazyLoad 插件加载组件和其他依赖项

python - Node.js 问题::Angular 未定义::找不到 bower 包::node gyp vs Python

AngularJS ng-repeat 带过滤器,用于大于数组中日期的日期

javascript - AngularJS 改进嵌套数组的过滤