我有一个带有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);
}
);
});
});
}
控制台日志"就绪",但列表不更新。我已经使用了ngzone尝试过,但是它仍然无法正常工作。只有当我打开sidemenu时,列表会更新,但之前却不是。有人知道如何解决这个问题?
我以前遇到过类似的问题。对我来说修复了它的方法是仅将变量更新/分配放在Zone.run()之间,因为在承诺解决您要更新值的承诺之后。
尝试:
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);
}
);
});