【问题标题】:ng-if inside ng-repeat is not filtering my arrayng-if 里面的 ng-repeat 没有过滤我的数组
【发布时间】:2017-07-04 16:31:07
【问题描述】:

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

[{"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" > 

【问题讨论】:

  • 您编辑了问题以包含解决方案。这不是 StackOverflow 的工作方式。将正确答案标记为已接受或在答案部分提供您自己的答案。

标签: angularjs angularjs-ng-repeat angular-ng-if


【解决方案1】:

我认为你需要这样检查,

<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>

【讨论】:

  • 我做了 什么也没发生,同样的问题...我有 7 个标签,我只想要 5 个
  • @ricardol 检查更新的答案。你需要使用 &&
【解决方案2】:

使用angularJS的filter功能。它从数组中选择项目的子集并将其作为新数组返回。

在过滤器中,表达式可以是字符串、对象或函数。这里我使用了表达式作为对象

让控制器变成这样

  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>

打工仔https://plnkr.co/edit/IUvaZbzHdV0M1s9TRzAq?p=preview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2016-09-23
    • 2018-07-18
    相关资源
    最近更新 更多