【发布时间】:2021-02-28 19:30:17
【问题描述】:
cList 的值为:
code value1 value2
ABC 01 test1
DEF 02 test2
GHI 03 test3
JKL 04 test4
MNO 05 test5
PQR 06 test6
STU 07 test7
VWX 08 test8
我的 component.ts 是这样的。数组列表。第 4 个列表添加到 cList1,第 5-8 个列表添加到 cList4。
cList: CaseInventorySummaryCustomDTO[] = [];
cList1: CaseInventorySummaryCustomDTO[] = [];
cList2: CaseInventorySummaryCustomDTO[] = [];
this.cList = this.data.cList;
for (let i = 0; i <= 3; i++) {
this.cList1.push(this.cList[i]);
}
for (let i = 4; i < this.cList.length; i++) {
this.cList2.push(this.cList[i]);
}
我的component.html如下:
<table>
<thead colspan="12">
Subject Specialities
</thead>
<tr *ngFor="let i of cList1; let j of cList2">
<td style="width: 4em">
{{i.code}}
</td>
<td style="width: 3em">
{{i.value1}}
</td>
<td colspan="2">
{{i.value2}}
</td>
<td style="width: 4em">
{{j.code}}
</td>
<td style="width: 3em">
{{j.value1}}
</td>
<td colspan="2">
{{j.value2}}
</td>
</tr>
</table>
我的预期输出是
Subject Specialities
ABC 01 test1 MNO 05 test5
DEF 02 test2 PQR 06 test6
GHI 03 test4 STU 07 test7
JKL 04 test4 VWX 08 test8
但我看到的是,
Subject Specialities
MNO 05 test5 MNO 05 test5
PQR 06 test6 PQR 06 test6
STU 07 test7 STU 07 test7
VWX 08 test8 VWX 08 test8
2 ngFor 不能在同一个 tr 上工作吗?还是我对上面的代码有误?有人可以帮忙吗。
【问题讨论】:
-
您不能在单个
*ngFor中迭代两个数组。它们需要被分成两个<tr>标签。 -
我不能分成 2 tr,因为值 2 有时可能是一个大段落。所以那个 tr 看起来很大,而另一个 tr 看起来很小。有没有相同的解决方法?
-
@JNPW,你不能使用
let i of cList1; let j of cList2使用let i of cList1; let index=index并在循环内cList2[index]代替“j”