angular - Angular 7 ngFor 迭代对象数组
问题描述
我无法遍历对象数组。在每个对象中,我都有一个名称,但我无法获取对象的文本或其中的名称。
我的 ts 文件中有以下代码
retrieveMultiple(config, "accounts")
.then(
(results) => {
const accounts: any[] = [];
for (let record of results.value) {
accounts.push(record);
}
this.accounts= accounts;
console.log(accounts[0].name);
console.log(accounts);
},
(error) => {
console.log(error);
}
我的 HTML 文件中有这个:
<p>Accounts:</p>
<ul>
<li *ngFor="let account of accounts | json">
{{ account.name }}
</li>
</ul>
解决方案
你的循环表达式中不应该有json
管道。将您的输出代码更改为:
<p>Accounts:</p>
<ul>
<li *ngFor="let account of accounts">
{{ account.name }}
</li>
</ul>
或者为了调试你可以使用这个:
<p>Accounts:</p>
<ul>
<li *ngFor="let account of accounts">
{{ account | json }}
</li>
</ul>
推荐阅读
- java - Spring Boot MockMvc 测试在使用“management.server.port”属性而不是已弃用的“management.port”时为执行器端点提供 404
- node.js - TypeError:User.generateAuthToken 不是函数
- flutter - Flutter Pub Get 命令失败
- css - 如何在剪贴板中复制颜色,reactjs
- php - Sylius 项目和 synfony 服务器 pkcs12 错误
- python - 是否可以将 reportlab.pdfgen.canvas.Canvas 转换为 SVG?
- android - R8 和 Proguard 规则
- c# - WPF TreeView中的多级多选
- php - Php/MySql 查找/替换 json 中的 utf-8 字符串
- html - jQuery:如何在单击按钮上将其传递给我的函数?