infomofo infomofo - 1 year ago 75
AngularJS Question

Is it possible to filter angular.js by containment in another array?

So if I have an array:

$scope.letters =

And another array

$scope.filterBy = ["b","c","d"];

And I want to have some ng-repeat to filter $scope.letters by only items that appear in $filterBy.

I want to be able to do something to the effect of:

<span ng-repeat="{{letter in letters|filter: in filterBy }} > {{}} </span>

And have it print b,c

I know this is a really stupid example, but is there a way to filter an angular.js expression based on the contents of another array object?

Answer Source

You should try something like that:


angular.module('Test', []);

function Ctrl($scope) {
  $scope.letters = [
    {id: 'a'},
    {id: 'b'},
    {id: 'c'}

  $scope.filterBy = ['b', 'c', 'd'];

  $scope.filteredLetters = function () {
    return $scope.letters.filter(function (letter) {
      return $scope.filterBy.indexOf( !== -1;

Ctrl.$inject = ['$scope'];


<div ng-repeat='letter in filteredLetters(letters)'>{{}}</div>

You can try live example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download