1

I have a filter:

angular.module('pb.ds.foundation').filter('notAvailable', function () {
  return function (items, name) {
    var arrayToReturn = [];

    for (var i = 0; i < items.length; i++) {
      if (items[i].code !== name) {
        arrayToReturn.push(items[i]);
      }
    }

    return arrayToReturn;
  };
});

which I wrote to filter out a given item from an ng-repeat:

ng-repeat="nio in icons.nucleoIcons | notAvailable: 'arrows-2_time'"

Now, however, I'd like to filter a second icon (string) from my repeater. Do I need to pipe the same filter a second time with the new value, or is there a way to pass the filter 1 or more values?

4
  • You can use multiple filters in the ng-repeat. See this page: stackoverflow.com/questions/14126905/… Commented Nov 23, 2016 at 20:13
  • I know I can (and have) used multiple filters, but never with the same filter twice. Commented Nov 23, 2016 at 20:15
  • It seems to work, but is that the "correct" way? Commented Nov 23, 2016 at 20:17
  • That's a good question Steve.You got me looking around for it and it doesn't appear that it's something frowned upon. Technically you could do the logic in your filter, inside the ng-repeat filter, so I suppose nothing is stopping you. All you've done is move the logic out into a filter, so keep it separate. Commented Nov 23, 2016 at 20:21

1 Answer 1

1

There are couple of ways you can accomplish this. You can pass a string to the filter, as you did, but you can also pass an array or a scope variable as well.

Here is a plnk that has the filter updated to accept a string, array, or scope variable.

angular.module('app', [])
.filter('notAvailable', function () {
  return function (items, name) {
    var arrayToReturn = [];

    if (Array.isArray(name)) {
      for (var i = 0; i < items.length; i++) {
        if (name.indexOf(items[i].code) === -1) {
          arrayToReturn.push(items[i]);
        }
      }
    } else {
      for (var i = 0; i < items.length; i++) {
        if (items[i].code !== name) {
          arrayToReturn.push(items[i]);
        }
      }
    }

    return arrayToReturn;
  };
})
Sign up to request clarification or add additional context in comments.

Comments

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.