【发布时间】:2018-04-27 23:38:55
【问题描述】:
我对 Ionic 和 Angular 都很陌生,所以如果这个问题没有多大意义,请原谅。
平台:Ionic 3。
我有一个提供者:rest.ts 如下
getLights() {
return new Promise(resolve => {
this.http.get(this.apiUrl+'/lights').subscribe(data => {
resolve(data);
}, err => {
console.log(err);
});
});
}
这将返回以下数据(敏感信息已删除)
{
"1": {
"state": {
"on": false,
"bri": 98,
"alert": "none",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Dimmable light",
"name": "Lion",
"modelid": "LWB010",
"manufacturername": "Philips",
},
"2": {
"state": {
"on": true,
"bri": 100,
"alert": "none",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Dimmable light",
"name": "Brutus",
"modelid": "LWB010",
"manufacturername": "Philips",
},
"3": {
"state": {
"on": true,
"bri": 254,
"hue": 8418,
"sat": 140,
"effect": "none",
"xy": [
0.4573,
0.41
],
"ct": 366,
"alert": "none",
"colormode": "ct",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Extended color light",
"name": "Hue lightstrip plus 1",
"modelid": "LST002",
"manufacturername": "Philips"
}
}
一个离子页面“列表”由:list.ts 组成
export class ListPage {
lights: any;
icons: string[];
constructor(public navCtrl: NavController,
public navParams: NavParams,
public restProvider: RestProvider) {
this.getLights();
}
getLights() {
this.restProvider.getLights()
.then(data => {
this.lights = data;
console.log(this.lights);
});
}
}
和 list.html :
<ion-content>
<ion-list inset>
<ion-item *ngFor="let light of lights">
<h2>{{light.name}}</h2>
<p>{{light.type}}</p>
</ion-item>
</ion-list>
</ion-content>
虽然控制台日志语句正确显示 JSON,但由于以下错误,我无法在 list.html 中使用 ngFor 对其进行迭代。
找不到“object”类型的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。
我意识到我需要将 JSON 转换为 javascript 数组,但不确定在哪里或如何执行此操作。任何指针将不胜感激。
【问题讨论】: