我有如下对象数组:
app.component.ts:
this.documents=[
{ employees: [ {name: "John Smith", age: 28},
{name: "Sarah Johnson", age: 32},
{name: "Mark Miller", age: 46}
]
},
{ employees: [ ] },
{ employees: [ {name: "Jimmy Colleen", age: 35},
{name: "Olivia Powell", age: 37}
]
},
]
app.component.html:
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Department</th>
<th scope="col">Name</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody *ngIf="documents.length">
<tr *ngFor="let doc of documents; let i = index">
<td>Department {{i + 1}}</td>
<td>Name {{}}</td>
<td>Age {{}}</td>
</tr>
</tbody>
</table>
我的问题是如何在HTML中为每个部门显示不同的名称及其年龄。我已经用谷歌搜索了这个问题,但是没有得到合适的答案。
答案 0 :(得分:1)
您在代码中没有提及任何部门名称。我添加了一些部门名称来填充数据。
示例代码:
app.component.ts
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Department</th>
<th scope="col">Name</th>
<th scope="col">Age</th>
</tr>
</thead>
<ng-container *ngIf="documents.length">
<tbody *ngFor="let doc of documents; let i = index">
<tr *ngFor="let employee of doc.employees;">
<td>{{employee.department}}</td>
<td>{{employee.name}}</td>
<td>{{employee.age}}</td>
</tr>
</tbody>
</ng-container>
</table>
app.component.html
values
希望这会有所帮助!