我有一个带有ngFor循环的离子列表。这是html:
<ion-list>
<ion-item *ngFor="let kiosk of kiosks" (click)="goToKioskProfilePage(kiosk)">
{{kiosk.name}}
</ion-item>
</ion-list>
这是构造函数:
kiosks: any;
constructor(public navCtrl: NavController, navParams: NavParams, public locationTracker: LocationTracker, public api: ApiService, public zone: NgZone) {
this.kiosks = [];
this.zone.run(() => {
this.locationTracker.getGeolocation().then(location => {
this.api.getClosestKiosks(location, constants.AMOUNT_OF_CLOSEST_KIOSKS).then(
data => {
console.log("ready");
this.kiosks = data['data'];
},
err => {
console.log(err);
}
);
});
});
}
控制台记录&#34;准备好&#34;但是列表没有更新。我已经用NgZone尝试了它,但它还没有用。只有当我打开侧边菜单列表更新时,才会更新。有人知道如何解决这个问题吗?
答案 0 :(得分:3)
之前我遇到过类似的问题。为我修复的是在zone.run()之间只放置变量更新/赋值,因为它是在promise解析你想要更新值之后。
尝试:
this.locationTracker.getGeolocation().then(location => {
this.api.getClosestKiosks(location, constants.AMOUNT_OF_CLOSEST_KIOSKS).then(
data => {
console.log("ready");
this.zone.run(() => {
this.kiosks = data['data'];
});
},
err => {
console.log(err);
}
);
});