Angular 2 ngFor 在同一个 tr 元素上
Angular 2 ngFor on the same tr element
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
中执行循环 2 数组。
您可以将元素 <ng-container>
用于第二个循环。
The Angular <ng-container>
is a grouping element that doesn't interfere with styles or layout because Angular doesn't put it in the DOM.
<tr *ngFor="let i of cList1;">
<ng-container *ngFor="let j of cList2">
...
</ng-container>
</tr>
问题的解决方案
<tr *ngFor="let item of cList1; let i = index">
<td style="width: 4em">
{{i.code}}
</td>
<td style="width: 3em">
{{i.value1}}
</td>
<td colspan="2">
{{i.value2}}
</td>
<td style="width: 4em">
{{cList2[i].code}}
</td>
<td style="width: 3em">
{{cList2[i].value1}}
</td>
<td colspan="2">
{{cList2[i].value2}}
</td>
</tr>
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
中执行循环 2 数组。
您可以将元素 <ng-container>
用于第二个循环。
The Angular
<ng-container>
is a grouping element that doesn't interfere with styles or layout because Angular doesn't put it in the DOM.
<tr *ngFor="let i of cList1;">
<ng-container *ngFor="let j of cList2">
...
</ng-container>
</tr>
问题的解决方案
<tr *ngFor="let item of cList1; let i = index">
<td style="width: 4em">
{{i.code}}
</td>
<td style="width: 3em">
{{i.value1}}
</td>
<td colspan="2">
{{i.value2}}
</td>
<td style="width: 4em">
{{cList2[i].code}}
</td>
<td style="width: 3em">
{{cList2[i].value1}}
</td>
<td colspan="2">
{{cList2[i].value2}}
</td>
</tr>