【发布时间】:2018-12-19 03:10:21
【问题描述】:
我在 URL 中有一个 JSON 数组,我正在尝试从中获取信息,以便可以在 ngFor 中使用它。如果我想获取链接或名称并在ngFor 中使用它,我做错了什么?因为我在console.log(this.straip) 中得到它,但不能在ngFor 中使用它。
Component.ts
export interface straipsnelis {
name: string;
link: string;
}
straip = {} as straipsnelis;
ngOnInit() {
this.http.get('this.url').subscribe((data:any) => {
this.straip.link = data.map((a:any)=>a.guid);
this.straip.name = data.map((a:any)=>a.slug);
console.log(this.straip);
})
}
HTML
<div *ngFor="let s of straip" class="cardukas">
<div class="left-photo">{{s.link}}</div>
<div class="right-info"></div>
</div>
控制台错误:
错误错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。
【问题讨论】:
-
请提供控制台结果
-
straip不是对象数组。 -
如何转换成对象数组?
-
{链接:数组(2),名称:数组(2)}链接:数组(2)0:{渲染:“life.optomeda.lt/?p=168”} 1:{渲染:“life.optomeda.lt/?p=1” } 长度:2 proto:数组(0)名称:数组(2)0:“h1-post-name-lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit”1: “pirmasis-tinklarascio-irasas”长度:2 proto:Array(0) proto:Object
-
Angular 不能循环一个Object,ngFor只支持iterables,你需要提供一个数组。
标签: javascript angular typescript angular-http