gpt4 book ai didi

javascript - 如何在 AngularJS 中列出多个项目的第一个索引行?

转载 作者:行者123 更新时间:2023-11-28 03:19:30 25 4
gpt4 key购买 nike

我希望创建一个特定于可以在多个项目中匹配的索引号的表。例如,如果我只想要索引 [0] 的项目,我希望它输出:

1a 2a
1b 2b

如何在我的 html 代码中实现此目的? html 按原样格式化,因此我可以写入连续的重复值。

var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function($scope) {
$scope.items = [];
$scope.items[0] = ["1a", "2a"];
$scope.items[1] = ["3a", "4a"];
$scope.items[2] = ["5a", null];
$scope.moreitems = [];
$scope.moreitems[0] = ["1b", "2b"];
$scope.moreitems[1] = ["3b", "4b"];
$scope.moreitems[2] = ["5b", null];
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<table class="table">
<tr ng-repeat="tr in items">
<td ng-repeat="td in tr track by $index">{{td}}</td>
</tr>
<tr ng-repeat="tr in moreitems">
<td ng-repeat="td in tr track by $index">{{td}}</td>
</tr>
</table>
</div>
</body>

最佳答案

一种方法是使用 ng-repeat-startng-repeat-end 在第一个循环中创建两行:

var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function($scope) {
$scope.items = [];
$scope.items[0] = ["1a", "2a"];
$scope.items[1] = ["3a", "4a"];
$scope.items[2] = ["5a", null];
$scope.moreitems = [];
$scope.moreitems[0] = ["1b", "2b"];
$scope.moreitems[1] = ["3b", "4b"];
$scope.moreitems[2] = ["5b", null];
});
<script src="//unpkg.com/angular/angular.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
<table class="table">
<tr ng-repeat-start="arr in items" ng-init="itemIndex=$index">
<td ng-repeat="data in arr track by $index">{{data}}</td>
</tr>
<tr ng-repeat-end>
<td ng-repeat="data in moreitems[itemIndex] track by $index">{{data}}</td>
</tr>
</table>
</body>

欲了解更多信息,请参阅

关于javascript - 如何在 AngularJS 中列出多个项目的第一个索引行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59294804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com