gpt4 book ai didi

angular - *ngFor 循环遍历数组 Angular6

转载 作者:行者123 更新时间:2023-12-04 00:03:53 25 4
gpt4 key购买 nike

我有这样的数组,

[[{"user":"1","nice":"0","sys":"1","CPU":"93","irq":"0"}, 
{"user":"1","nice":"0","sys":"1","CPU":"92","irq":"0"},
{"user":"1","nice":"0","sys":"1","CPU":"92","irq":"0"},
{"user":"1","nice":"0","sys":"1","CPU":"92","irq":"0"}],
[{"user":"0","nice":"0","sys":"0","CPU":"91","irq":"0"},
{"user":"0","nice":"0","sys":"1","CPU":"91","irq":"0"},
{"user":"1","nice":"0","sys":"0","CPU":"91","irq":"0"},
{"user":"0","nice":"0","sys":"0","CPU":"90","irq":"0"}]]

我想使用 ngFor 循环遍历 HTML 表中的这个对象数组

<table class="table table-striped mt-5">
<thead>
<tr>
<th scope="col">User</th>
<th scope="col">Nice</th>
<th scope="col">Sys</th>
<th scope="col">CPU</th>
<th scope="col">IRQ</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let post of posts">
<td>{{post.user}}</td>
<td>{{post.nice}}</td>
<td>{{post.sys}}</td>
<td>{{post.CPU}}</td>
<td>{{post.irq}}</td>
</tr>
</tbody>
</table>

但我的代码不起作用

最佳答案

您可以使用 ng-container保存一个 ngFor 并在 tr 标记中执行第二个 ngFor,如下所示:

<tbody>
<ng-container *ngFor="let group of posts">
<tr *ngFor="let post of group">
<td>{{post.user}}</td>
<td>{{post.nice}}</td>
<td>{{post.sys}}</td>
<td>{{post.CPU}}</td>
<td>{{post.irq}}</td>
</tr>
</ng-container>
</tbody>

关于angular - *ngFor 循环遍历数组 Angular6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52845905/

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