1

I created a filter that I want to pass an item property, and not the item itself. Is that possible?

The following does not work (item.param1 fails):

ng-reapeat="item in items | filter : fnFilter(item.param1)"

$scope.fnFilter = function(value) {
    return value == "test";
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 1
    check this: http://stackoverflow.com/a/20292780/5376197 – Ceylan Mumun Kocabaş Jun 08 '16 at 09:41
  • and the answer is what? the linked question does *not* tell how to hand a property parameter of a `ng-repeat` element to a filter function. But that's indeed the question here. – membersound Jun 08 '16 at 09:46
  • _"I created a filter"_ You created a predicate for the filter filter. Why don't you just use `fnFilter` as wrapper for the "real" predicate? Or create an actual filter. – a better oliver Jun 08 '16 at 12:32

1 Answers1

1

Your question is quite unclear sdince you don't tell really what is your goal.

So if i just restrict to what i see there and following the link Angular filter exactly on object key already provided by @CeylanMumumKocabas you would have

ng-repeat="item in items | filter:{'param1':  'test'}"

Now let's consider you want something more complex : the only way i see would be to pass the name of the attribute to the filter :

ng-reapeat="item in items | myFilter:'param1'"

  myApp.filter('myFilter', function () {  
      return function(inputs,attributeName) {
      var output = [];
      angular.forEach(inputs, function (input) {
        if (input[attributeName] == 'test')
            output.push(input);
        });
       return output;
   };
});

Note that if you want to go more than one level, you'll have to use $eval or make add some code for this to work.

Community
  • 1
  • 1
Walfrat
  • 5,363
  • 1
  • 16
  • 35
  • That's close: I'd like to pass the value ofr `item.param1` to the filter. And then let the filter function decide to return true or false based on multiple conditions. I do not want to filter that property only based on one single value, like you did with `test` – membersound Jun 08 '16 at 10:03
  • i just took from your sample, i don't think you can pass an attribute to the filter, that's why i'm passing the name of the attribute, so i can still match the need to be able to filter on a property that can change. – Walfrat Jun 08 '16 at 10:13