如何过滤 angularJS 中的多个动态值

How to filter multiple dynamic values in angularJS

就像 This 问题中的问题提供者一样,我正在构建一个过滤多个值的过滤器。

但是,在我的情况下,我还希望从数组中动态创建过滤器。

所以,对于这个例子,而不是

<input type="checkbox" ng-model="genrefilters.action" />Action

<input type="checkbox" ng-model="genrefilters.family" />Family

我想像这样 ng-repeat 来创建输入框:

<div ng-repeat="distinctgenre in distinctgenres'">
    <input type="checkbox" ng-model="genrefilters.distinctgenre"/{{distinctgenre}}
</div>

但是,当我这样做时,我的过滤器与原始问题中的相同:

.filter('bygenre', function () {
return function (movies, genrefilters) {
    var items = {
        genrefilters: genrefilters,
        out: []
    };
    angular.forEach(movies, function (value, key) {
        if (this.genrefilters[value.genre] === true) {   // <=== THIS LINE ERRORS OUT
            this.out.push(value);
        }
    }, items);
    return items.out;
};

错误,类型错误:无法读取未定义的 属性 'Action'。

我是这样使用过滤器的。

<tr ng-repeat="movie in displayedmovies | byCohort:genrefilters">
    {{movie}}

如有任何帮助,我们将不胜感激。

感谢 dfsq 对括号符号的提醒。这似乎是主要问题。 (我还有其他一些愚蠢的命名约定问题)

您应该使用括号表示法生成过滤器输入:

<div ng-repeat="distinctgenre in distinctgenres'">
    <input type="checkbox" ng-model="genrefilters[distinctgenre]" /> {{distinctgenre}}
</div>