AngularJS按数组中的数组过滤

时间:2016-02-22 22:45:02

标签: angularjs angularjs-filter

我有一个数组:

$scope.posts = [
  {"from":"Aaron", "groups":[1,2,3]},
  {"from":"Byron", "groups":[1,2,4]},
  {"from":"Caren", "groups":[1,3,5]}
]

我想在Controller中按组过滤。例如,如果我只想要群组中包含2的帖子,则只显示Aaron和Byron帖子。

我尝试使用按钮ng-click功能执行此操作:

<button ng-click="filterByGroup(2)">Filter</button>

在控制器中,

.controller ('Ctrl',$scope, $filter) {

    $scope.posts = [
      {"from":"Aaron", "groups":[1,2,3]},
      {"from":"Byron", "groups":[1,2,4]},
      {"from":"Caren", "groups":[1,3,5]}
    ];

    $scope.filterByGroup = function (group_no){
       $filter('filter')($scope.posts,?,group_no)
    }
}

我知道我可以在?中做一个功能,但我不知道该怎么做。正在考虑做indexOf,但我无法做groups.IndexOf(group_no)

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你不能做

  <div ng-repeat="post in posts | filter: { groups: 2 } : true">
    {{ post.from }}
  </div>

小提琴 - https://jsfiddle.net/0fqavhbu/