2

Just like the question provider in This question, I am building a filter that filters on multiple values.

However, in my situation, I also want to have the filters created dynamically from an array.

So, for this example, instead of

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

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

I want to ng-repeat like this to create the input boxes:

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

However, when I do this, my filter, which is the same as in the original question:

.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;
};

Errors out, with TypeError: Cannot read property 'Action' of undefined.

I am using the filter like so.

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

Any assistance would be appreciated.

Thanks to dfsq for the reminder on bracket notation. That seems to be the main issue. (I also had some other dumb naming convention issues)

1 Answer 1

1

You should generate filter inputs using bracket notation:

<div ng-repeat="distinctgenre in distinctgenres'">
    <input type="checkbox" ng-model="genrefilters[distinctgenre]" /> {{distinctgenre}}
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Goddammit, I knew it was gonna be something dumb. I'll check it when I get back from lunch but this is probably the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.