Sámal Rasmussen
Sámal Rasmussen

Reputation: 3495

Pass array of filters to a directive

I need to do something similar to this, where I want to determine a class from a value inside a ng-repeat inside a directive:

<div ng-repeat="value in data">
  <div class="value | classFilters[$index]">
  </div>
</div>

The directive must be able to support different filters for different data. So I'm thinking I want to pass one filter per value in data.

I am able to make an array of filters something like this:

this.$scope.classFilters = [
  this.$filter('filter1'),
  this.$filter('filter2'),
  this.$filter('filter3')
];

And pass that to the directive. But then I get an injector error with "Unknown provider: classFiltersFilterProvider".

Anyone have a fixed solution or an idea for an alternative solution?

Upvotes: 1

Views: 362

Answers (1)

Shuhei Kagawa
Shuhei Kagawa

Reputation: 4777

Use filters as functions.

<div ng-repeat="value in data">
  <div ng-class="classFilters[$index](value)">
  </div>
</div>

Upvotes: 2

Related Questions