javascript - 如何从数字数组中过滤 ngRepeat 中的项目?

标签 javascript angularjs arrays

我正在努力寻找一种基于数字数组在 ng-repeat 中过滤一组结果的方法。这些数字可以代表我想看到或不想看到的结果。我可以在 View 中使用过滤器并按一个数字进行过滤,这很有效,但我想在那里放置一个数组并按数组进行过滤。我不关心过滤器是在 View 、 Controller 还是单独的过滤器中。

下面的代码成功过滤了论坛id为60的话题

  <div ng-repeat="topic in topics | filter:{forum_id:60}">
    Forum ID: {{topic.forum_id}} Forum Name: {{topic.forum_title}}
  </div>

我想按数组过滤

  <div ng-repeat="topic in topics | filter:{forum_id:ARRAY_HERE}">
    Forum ID: {{topic.forum_id}} Forum Name: {{topic.forum_title}}
  </div>

全文在这里: https://plnkr.co/edit/dCZ7DV?p=preview

最佳答案

将自己的函数传递给过滤器:

<div ng-repeat="topic in topics | filter:myFilterMethod(topic)">
    Forum ID: {{topic.forum_id}} Forum Name: {{topic.forum_title}}
</div>

在你的 Controller 中:

$scope.myFilterMethod = function() {
    return function(e) {
        return $scope.filterBy.includes(e.forum_id);
    }
}

updated plunkr

关于javascript - 如何从数字数组中过滤 ngRepeat 中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762216/

相关文章:

javascript - React-native-vision-camera 无法访问后面的普通相机

javascript - 如何匹配以空格和字符开头的所有单词

javascript - 在 AngularJS 函数中调用 $http

url-routing - 在Angular.js中的子目录中路由

java - 将 long[] 更改为 Set。错误消息 : The method addAll in the type Collections is not applicable for the arguments (Set<Long>, 长[])

c - 如何在 C 中定义函数数组

javascript - 优化我的Node.js/Js/Socket射击游戏

javascript - 如何创建新的回调函数

javascript - 使用 AngularJS 更新 cookie

javascript - 如何合并多个多维JSON文件?