【问题标题】:ng-if and ng-repeat to compare in tableng-if 和 ng-repeat 在表格中进行比较
【发布时间】:2016-08-29 07:44:03
【问题描述】:

我有一个 id 列 (1,2,3,4,5) 的表,并且我有一个元素数组。 $scope.elements 的值为 - 1,3。

<ul ng-repeat="x in elements>
<li>{{x}}</li>
</ul>

使用它,我必须将元素数组中的值与表中的 id 进行比较。如果两个 idd 匹配,我必须隐藏该行。

当我使用ng-if 时,由于我使用 ng-repeat,每次迭代都会创建多行...

谁能帮我解决这个问题...


更新: 隐藏2 = [1,3,4] ng-repeat="x in [hides2] track by $index" ng-if="x!=3"

但是表格显示了值,(它没有隐藏 1,3,4 记录) [enter image description here]1

【问题讨论】:

  • 您的问题到底是什么?
  • @Rakeschand 我有一个 id 为 1,2,3,4,5 的表,我有一个包含一些数字的元素数组。我必须将这些数组编号与表 id 进行比较。当找到匹配项时,我必须隐藏该表行...
  • 你试过我的答案了吗?

标签: angularjs angularjs-ng-repeat


【解决方案1】:

我不知道你是怎么使用 ng-if 的,但是它使用起来很简单,这里是一个简单的例子

 angular.module("app",[]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>

<body ng-app="app">
  <div ng-repeat="item in [1,2,3,4,5,6,1,2,4,5] track by $index" ng-if="item !== 1">{{item}}</div>

【讨论】:

    【解决方案2】:

    这里试试这个..在控制器中创建一个函数如下

    $scope.elements = [1,2,3,4,5];
    $scope.checkMatch = function(id){
       if($scope.elements.indexOf(id) !== -1){
         return true;
       }else{
         return false;
       }
    }
    

    在您的模板中,在 ng-if 中调用此方法,如下所示,并在 checkMatch 中传递 id...

    <ul ng-repeat="x in elements>
    <li ng-if="checkMatch(x)">{{x}}</li>
    </ul>
    

    【讨论】:

    • 你通过这个解决方案得到了什么结果?您需要上传整个代码或创建一个 plunkr。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多