gpt4 book ai didi

arrays - 如何使用 ngFor Angular 2 在对象数组中显示数组

转载 作者:太空狗 更新时间:2023-10-29 17:39:33 25 4
gpt4 key购买 nike

我需要显示位于对象数组内的数组质量。我尝试使用下面的代码在 ngFor 中调用它。我对 ngFor 的使用有问题吗?

import { Component} from '@angular/core';

@Component({
selector: 'my-app',
template: `<table>
<thead>
<tr>
<th>Name</th>
<th>Quality1</th>
<th>Quality2</th>
</tr>
</thead>
<tbody>
<tr *ngFor"let item of people">
<td>{{item.Name}}</td>
<td *ngFor"let item of people.quality">item.quality1</td>
<td *ngFor"let item of people.quality">item.quality2/td>
</tr>
</tbody>
</table>
})

export class AppComponent{
people: any = [{Name:"John", quality:[{quality1: "nice", quality2: "humble"}]}, {Name:"Dave", quality:[{quality1: "kind", quality2: "caring"}]} ];
}

截至目前,只有名称出现在表格中,但我还希望出现质量。

最佳答案

我们可以简单的通过修改第二个循环来迭代里面的循环

        <tr *ngFor="let item of people">
<td>{{item.Name}}</td>
<td *ngFor="let quality of item.quality">{{ quality }}</td>
<td *ngFor="let quality2 of item.quality2">{{quality2}}</td>
</tr>

关于arrays - 如何使用 ngFor Angular 2 在对象数组中显示数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40759703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com