我试图用不同范围变量的值填充同一个表。 A 和 B 的长度始终相同。
$scope.a = [1,2,3]
$scope.b = [4,5,6]
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="itemA in a, itemB in b"> <!-- How should be this ng-repeat?-->
<td>{{itemA}}</td>
<td>{{itemB}}</td>
</tr>
</tbody>
</table>
结果应该是这样的表格:
A - B
1 - 4
2 - 5
3 - 6
最佳答案
如果我们可以假设两个数组总是具有相同的长度:
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="itemA in a track by $index">
<td>{{itemA}}</td> <!--Could also be a[$index] -->
<td>{{b[$index]}}</td>
</tr>
</tbody>
</table>
它所做的只是通过索引跟踪数组,并使用该索引获取 B 中必要的元素。
关于javascript - 多次 ng-repeat 来填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38509229/