【发布时间】:2021-11-07 20:40:37
【问题描述】:
我从 api 获取数据作为对象。我正在尝试通过 ngFor 在 component.html 中使用,但出现此错误
找不到“test”类型的不同支持对象“[object Object]”。 NgFor 只支持绑定到数组等Iterables。
所以我的对象是这样的:
{id: 9, name: 'test', username: 'testmg', email: 'test@gmail.com', email_verified_at: null, ...}
我该如何解决这个问题?
profile.component.ts
username:any
playerData:any
constructor(private dataService: DataService,private router: ActivatedRoute) { }
ngOnInit(): void {
this.username = this.router.snapshot.params.username
this.getOne()
}
getOne(){
this.dataService.getProfile(this.username).subscribe(res=>{
this.playerData = res
})
}
profile.component.html
<div *ngFor="let item of playerData">
{{item}}
</div>
【问题讨论】:
-
你对
{{item}}9, est@gmail.com...有什么期望? -
实际上,如果我输入
{{item.id}}我想查看 id 的值,或者如果我输入{{item.name}}我想查看名称的值
标签: angular typescript rxjs ngfor