typescript - 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>
答案#1
你不应该在循环表达式中使用 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>
答案#2
https://angular.io/api/common/NgForOf
retrieveMultiple(config, "accounts")
.then(
(results) => {
this.accounts = results.value;
console.log(this.accounts[0].name);
console.log(this.accounts);
},
(error) => {
console.log(error);
}
)
<p>Accounts:</p>
<ul>
<li *ngFor="let account of accounts">
{{ account.name }}
</li>
</ul>
