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

前端 未结 3 1807
孤街浪徒
孤街浪徒 2020-11-30 03:24

So if I have an array:

$scope.letters = 
[{\"id\":\"a\"},
{\"id\":\"b\"},
{\"id\":\"c\"}];

And another array

$scope.filter         


        
3条回答
  •  臣服心动
    2020-11-30 03:59

    You should try something like that:

    JS:

    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(letter.id) !== -1;
        });
      };
    }
    
    Ctrl.$inject = ['$scope'];
    

    HTML:

    {{letter.id}}

    You can try live example.

提交回复
热议问题