ng-if 里面的 ng-repeat 没有过滤我的数组

ng-if inside ng-repeat is not filtering my array

大家好,我正在尝试在我的类型数组中显示带有值的选项卡,其中包含

[{"label":"wlan1","type":"wlan1"},
 {"label":"br-wlan","type":"br-wlan"},
 {"label":"tun_cas","type":"tun_cas"},
 {"label":"nfacct_bytes","type":"nfacct_bytes"},
 {"label":"wlan0","type":"wlan0"},
 {"label":"lte0","type":"lte0"},
 {"label":"nfacct_packets","type":"nfacct_packets"}]

在我看来,我不想显示类型 nfacct_bytes 和 nfacct_packets,为此我正在做:

<tab ng-if="type.label !== 'nfacct_bytes' || type.label !== 'nfacct_packets'" 
     ng-repeat="type in dataGraph.network.types" heading="{{type.label}}"
     select="changeSubTab(type.type)" disable="!tabClick" >                                                                                                

解决方案:

<tab ng-if="type.label !== 'nfacct_bytes' && type.label !== 'nfacct_packets'" 
     ng-repeat="type in dataGraph.network.types" heading="{{type.label}}"
     select="changeSubTab(type.type)" disable="!tabClick" > 

我认为你需要这样检查,

<tab ng-if="type.label !== 'nfacct_bytes' && type.label !== 'nfacct_packets' "> 
</tab>

演示

var myApp=angular.module('myApp',[]);
 myApp.controller('thecontroller',function($scope){
       $scope.dataGraph = {};
       $scope.dataGraph.network = {};
         $scope.dataGraph.network.types = [{"label":"wlan1","type":"wlan1"},{"label":"br-wlan","type":"br-wlan"},{"label":"tun_cas","type":"tun_cas"},{"label":"nfacct_bytes","type":"nfacct_bytes"},{"label":"wlan0","type":"wlan0"},{"label":"lte0","type":"lte0"},{"label":"nfacct_packets","type":"nfacct_packets"}];
});
 
<!DOCTYPE html>
    <html>
        <head>
          <title>ng-Messages Service</title>
          <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
            <script src='https://code.angularjs.org/1.5.0-rc.0/angular.min.js'></script>
        </head>
        <body ng-app="myApp">
            <div ng-controller='thecontroller'>
            <div  ng-repeat="type in dataGraph.network.types">
           <div ng-if="type.label !== 'nfacct_bytes' && type.label !== 'nfacct_packets' "> {{type.label}}
            </div>
            </div>
            </div>
        </body>
    </html>

使用angularJS的过滤函数。它从数组中选择项目的子集并将其 returns 作为一个新数组。

过滤器中的表达式可以是字符串、对象或函数。这里我使用了 expression as object

让控制器变成这样

  app.controller('MainCtrl', function($scope) {
    $scope.Values = 
     [{"label":"wlan1","type":"wlan1"},
     {"label":"br-wlan","type":"br-wlan"},
     {"label":"tun_cas","type":"tun_cas"},
     {"label":"nfacct_bytes","type":"nfacct_bytes"},
     {"label":"wlan0","type":"wlan0"},
     {"label":"lte0","type":"lte0"},
    {"label":"nfacct_packets","type":"nfacct_packets"}];
});

所需HTML

  <div ng-repeat="value in Values |filter: {type: '!nfacct_bytes'}">
      {{value.label}}
  </div>

工作的 plunker https://plnkr.co/edit/IUvaZbzHdV0M1s9TRzAq?p=preview