2 回答
![?](http://img1.sycdn.imooc.com/54586453000163bd02200220-100-100.jpg)
TA贡献1852条经验 获得超7个赞
您可以通过keyvalue pipe访问详细信息对象来迭代对象键/值作为内部 for 循环 。
网址:
<table>
<tr>
<th>Firstname</th>
<th>lastname</th>
<th>email_address</th>
<th>phone_number</th>
</tr>
<tr *ngFor="let listItem of yourList" >
<td *ngFor="let innerItem of listItem.details | keyvalue">
{{innerItem.value}
</td>
</tr>
</table>
填充列表
yourList = [{
"id": 9,
"event_id": 13,
"details": {
"firstname": "Ralph",
"lastname": "Marvin",
"email_address": "ralphmarvin@email.com",
"phone_number": "0987654321"
}
}, {
"id": 10,
"event_id": 13,
"details": {
"firstname": "John",
"lastname": "X Doe",
"email_address": "john_x_doe120@xmail.com",
"phone_number": "0009876543"
}
}, {
"id": 11,
"event_id": 13,
"details": {
"firstname": "Wari",
"lastname": "Gumah",
"email_address": "gumahwarihanna@eeee.com",
"phone_number": "120029182765"
}
}]
![?](http://img1.sycdn.imooc.com/54dc06a60001ef0401000100-100-100.jpg)
TA贡献1835条经验 获得超7个赞
您可以在组件上实现一些逻辑来获取一组字段。它看起来像这样:(我假设项目作为 @Input() 出现在您的组件中,但它可能是另一个来源)
@Input() items: Item[];
fields: Set<string>;
ngOnChanges(changes) {
if(!changes.items || !this.items) return;
this.fields= new Set(this.items.flatMap(item => Object.keys(item.details)))
}
然后在模板中
<tr *ngFor="let item of items">
<td *ngFor="let field of fields">{{item.details[field]}}</td>
</tr>
添加回答
举报