在ng-repeat语句中使用Or条件

时间:2019-07-23 20:45:15

标签: angularjs angularjs-ng-repeat

如何在ng-repeat中使用Or条件。我目前有

<div ng-repeat="waterSample in viewRequest.data.REV_SAMPLE_CMQREQUEST_WATER_SAMPLE">

有时可以是viewRequest.data.REV_SAMPLE_CMQREQUEST。它应该寻找viewRequest.data.REV_SAMPLE_CMQREQUEST_WATER_SAMPLE or viewRequest.data.REV_SAMPLE_CMQREQUEST我该如何解决

3 个答案:

答案 0 :(得分:1)

要获得预期结果,请使用以下三元条件来检查属性:在viewRequest.data中使用REV_SAMPLE_CMQREQUEST_WATER_SAMPLE,并根据条件使用REV_SAMPLE_CMQREQUEST_WATER_SAMPLE或REV_SAMPLE_CMQREQUEST

     <div ng-repeat="waterSample in viewRequest.data.hasOwnProperty('REV_SAMPLE_CMQREQUEST_WATER_SAMPLE')? viewRequest.data.REV_SAMPLE_CMQREQUEST_WATER_SAMPLE : viewRequest.data.REV_SAMPLE_CMQREQUEST">
{{wareSample}}
      </div>

工作代码示例

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $scope.viewRequest = {
    data: {
      REV_SAMPLE_CMQREQUEST_WATER_SAMPLE : [
        {id: 1, name: "test1"},
        {id: 2, name: "test2"}
      ],
      REV_SAMPLE_CMQREQUEST:[
        {id: 100, name: "ABC1"},
        {id: 200, name: "ABC2"}
      ]
    }
  }
  
  $scope.conditionalArr = $scope.viewRequest.data.hasOwnProperty('REV_SAMPLE_CMQREQUEST_WATER_SAMPLE')? $scope.viewRequest.data.REV_SAMPLE_CMQREQUEST_WATER_SAMPLE : $scope.viewRequest.data.REV_SAMPLE_CMQREQUEST
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl"> 

  <div ng-repeat="waterSample in viewRequest.data.hasOwnProperty('REV_SAMPLE_CMQREQUEST_WATER_SAMPLE')? viewRequest.data.REV_SAMPLE_CMQREQUEST_WATER_SAMPLE : viewRequest.data.REV_SAMPLE_CMQREQUEST">
    {{waterSample.id}} {{waterSample.name}}
  </div>

</div>



</body>

codepen-https://codepen.io/nagasai/pen/GVZRem?editors=1010

答案 1 :(得分:0)

一种方法是使用ng-show,如下所示:

<div ng-show="viewRequest.data.REV_SAMPLE_CMQREQUEST_WATER_SAMPLE" 
    ng-repeat="waterSample in viewRequest.data.REV_SAMPLE_CMQREQUEST_WATER_SAMPLE">
<div ng-show="!viewRequest.data.REV_SAMPLE_CMQREQUEST_WATER_SAMPLE" 
    ng-repeat="waterSample in viewRequest.data.REV_SAMPLE_CMQREQUEST">

答案 2 :(得分:0)

我认为更简单,更干净的方法是在控制器中连接这两个数组,然后在ng-repeat中使用它。