我正在尝试从其中包含数组的 Json 动态创建 html 表。我遇到的问题是尝试<td>
从子数组创建 s 时。下面是我想要完成的一个例子。
我应该使用<ng-container *ngFor="let m of l.dArray">
吗?
myArray = [ {
"a": "01",
"b": "01",
"c": "03",
"dArray": [
{
"d-1": 1,
"d-2": 2,
"d-3": "string1"
},
{
"d-1": 2,
"d-2": 1,
"d-3": "string2"
},
{
"d-1": 3,
"d-2": 4,
"d-3": "string3"
}
]
}
]
模板:
<table>
<thead>
<tr><th>A</th></tr>
<tr><th>B</th></tr>
<tr><th>C</th></tr>
<tr><th>D-1</th></tr>
<tr><th>D-2</th></tr>
<tr><th>D-3</th></tr>
</thead>
<tbody>
<tr *ngFor="let l of myArray">
<td>{{l.a}}</td>
<td>{{l.b}}</td>
<td>{{l.c}}</td>
<ng-container *ngFor="let m of l.dArray">
<td>
<div *ngIf="m.d-1 === 1; else my1Else">
{{ m.d-3 }}%
</div>
<ng-template #my1Else>
N/A
</ng-template>
</td>
<td>
<div *ngIf="m.d-1 === 2; else my2Else">
{{ m.d-3 }}%
</div>
<ng-template #my2Else>
N/A
</ng-template>
</td>
<td>
<div *ngIf="m.d-1 === 1; else my3Else">
{{ m.d-3 }}%
</div>
<ng-template #my3Else>
N/A
</ng-template>
</td>
</ng-container>
</tr>
</tbody>
</table>
预期结果:
----------------------------
A | B | C | D-1 | D-2 | D-3
01 | 01| 03 | string1 | string2 | string3
actual outcome:
----------------------------
A | B | C