如何使用 ng-repeat 在表格中显示数组

菜鸟编码器

与其他 JSON 数据不同,我有两个简单的数组。

$scope.land = [ elephant, tiger, zebra ];
$scope.water = [ fish , octopus, crab];

我想使用ng-repeat. 我试过了,但数据没有以正确的格式显示。

<table class="table table-dark">
    <thead>
        <tr>
            <th scope="col">LAND</th>
            <th scope="col">WATER</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="l in land track by $index">
            <td>{{l}}</td>
            <td>fish</td>
        </tr>
    </tbody>
</table> 

编辑:- 在表格中显示垂直的数据错误 在 此处输入图像描述

新数据 :-

$scope.land = [ 'tiger in zoo', 'tiger in jungle', 'zebra'];
 $scope.water = [ 'fish in pond'];
阿卜杜勒卡里姆·埃尔·阿梅尔

我认为您正在尝试显示彼此相邻的两个数组的元素(如果我错了,请纠正我)。

这是一个简单的方法:

angular.module('myApp', [])
.controller('TestCtrl', function ($scope) {
    $scope.land = [ 'tiger in zoo', 'tiger in jungle', 'zebra'];
    $scope.water = [ 'fish in pond'];
    $scope.combined = [];
    var maxLength = ($scope.land.length > $scope.water.length) ? $scope.land.length : $scope.water.length;
    //length is to be determined
    for(var index = 0; index < maxLength ; index++) {
    	$scope.combined.push({
      	land: ($scope.land[index]) ?  $scope.land[index] : '',
        water: ($scope.water[index]) ? $scope.water[index] : ''
      });
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="TestCtrl">
  <table class="table table-dark">
    <thead>
      <tr>
        <th scope="col">LAND</th>
        <th scope="col">WATER</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="item in combined">
        <td>{{item.land}}</td>
        <td>{{item.water}}</td>
      </tr>
    </tbody>
  </table> 
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何替换使用ng-repeat显示的数组中的对象?

如何使用 ng-repeat 显示数组/对象

AngularJS:如何使用ng-repeat显示父数组中包含的数组的所有元素

如何使用ng-repeat遍历JSON中的数组数组?

如何使用ng-repeat显示数组中对象的特定属性

使用 ng-repeat 将鼠标悬停在表格中的文本上时显示图像

ng-repeat中的数组未显示

AngularJS-如何在ng-repeat中单击父数组时显示嵌套数组

如何从ng-repeat获取数组项?

如何使用ng-repeat显示嵌套对象

当使用ng-repeat时,如何仅显示更改的字段?

如何使用ng-repeat和ng-if在AngularJS中显示或隐藏某些HTML标签

如何在子ng-repeat中获取父范围数组/ ng-repeat的索引

AngularJS-ng-repeat-如何使用ID创建数组

如何在多个对象数组上使用ng-repeat?

如何使用ng-repeat遍历对象内的数组

使用ng-repeat渲染Angular中的数组表数组

无法使用ng-repeat遍历数组中的数组

使用ng-repeat迭代AngularJS中的数组数组

使用ng-repeat遍历对象数组

如何在ng-repeat中显示数组的特定字母项目?

如何在单个ng-repeat中显示2个不同数组的值?

如何在Angularjs中的ng-repeat中显示单行

需要在使用 ng-repeat 时在对象内的数组中显示对象

表格行中的 ng-repeat 不显示行

如何使用 forEach 从 ng-repeat 中删除 json 对象

如何使用$ index更改ng-repeat中的类?

如何使用angularjs在ng-repeat中打印Json数据?

如何使用jquery滑块过滤ng-repeat中的数据?