使用以下代码时出现此错误:
无法找到'object'类型的不同支持对象'[object Object]'。
在执行嵌套的ngFor时,有没有办法解决这个问题?
import { Component,OnInit } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div id="results">
<div *ngFor="let mem of members">
{{mem.id}}
<div class="card-container">
<div *ngFor="let case of mem.cases">
{{case.id}}
</div>
</div>
</div>
</div>
`,
})
export class AppComponent implements OnInit {
public members = { "members": [
{ "name" : "Ana", "id" : "001",
"cases" : [
{"id" : "992", "casenumber" : "882"},
{"id" : "242", "casenumber" : "344"},
]} ,
{ "name" : "Ina", "id" : "002",
"cases" : [
{"id" : "532", "casenumber" : "234"},
{"id" : "734", "casenumber" : "346"},
]} ,
{ "name" : "Ora", "id" : "003",
"cases" : [
{"id" : "235", "casenumber" : "974"},
{"id" : "458", "casenumber" : "125"},
]}
]
}
ngOnInit(){
console.log(this.members);
}
}
答案 0 :(得分:17)
您的顶级迭代错误,您要迭代的数组位于members.members
内,因此它应该是:
<div *ngFor="let mem of members.members"> // here
{{mem.id}}
<div class="card-container">
<div *ngFor="let case of mem.cases">
{{case.id}}
</div>
</div>
</div>
答案 1 :(得分:4)
你遍历一个objet而不是一个数组,所以你应该通过切换到指向该数组:
<div *ngFor="let mem of members.members">
答案 2 :(得分:3)
更改
<div *ngFor="let mem of members">
到
<div *ngFor="let mem of members.members">