javascript - 显示具有匹配 id 的项目

标签 javascript angularjs

我想创建一个仅显示具有匹配 id 的项目的过滤器,目前它只能以相反的方式工作。 hideIds 函数中需要更改哪些内容?

js fiddle

异常

var app = angular.module('m', []);

app.controller('myCtrl', function ($scope) {
  $scope.subtree = [{
    id: 1,
    name: susan
}, {
    id: 2,
    name: peter
}, {
    id: 3,
    name: marc
}, {
    id: 4,
    name: julia
}, {
    id: 5,
    name: sylvia
}, {
    id: 6,
    name: martin
 }];


  var arr = [1,2,6];


$scope.hideIds = function (ids) {
    return function (item) {
        return arr.indexOf(item.id) === -1;
     }
   };
});

模板

最佳答案

您可以执行!== -1,而不是执行=== -1。如果数组不包含该元素,.indexOf 将返回 -1。这意味着 [1,2,6].indexOf(5) 返回 -1,但 [1,2,6].indexOf(1) 返回 0

关于javascript - 显示具有匹配 id 的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25793221/

相关文章:

javascript - Mongoose - 保存模型数组并 promise 全部

javascript - 有没有办法增加 Google Chrome 中 localStorage 的大小以避免 QUOTA_EXCEEDED ERR : DOM Exception 22

javascript - 不需要的 MobileSafari 'Open this page in "App Store”?对话

javascript - 更改 firebase.database 内的变量

javascript - 无法更新当前状态的数组

javascript - 为什么我在这个 Angular 应用程序代码中得到 "Incorrect injection token!"?

javascript - 如何以 Angular 访问另一个 View 的 $scope?

javascript - 如何在angularjs中的对象数组中推送i次

javascript - 当我使用 ng-options 为什么我选择的没有值(value)

javascript - 如何在AngularJS中的多个表单 View 之间传递数据