我有 Ionic2
应用程序和 SideMenu
模板,在 rootPage
上我有这段代码
export class HomePage {
products: any = [];
constructor(public navCtrl: NavController, public navParams: NavParams, private woo: WooCommerce) {
}
ionViewDidLoad() {
this.woo.Fetch('products', function (res) {
this.products = res.products;
//console.log(this.products[0]);
}.bind(this));
}
ngOnInit() {
}
}
其中 WooCommerce
是 WooCommerce-Nodejs
上的提供者包装器
export class WooCommerce {
woo: any = null;
constructor(public http: Http, private util: Utilities) {
this.woo = WooCommerceAPI();
}
Fetch(what, callback) {
return this.woo.getAsync(what).then(function (result) {
callback(JSON.parse(result.body));
});
}
}
在我的 page.ts
ionViewDidLoad() {
this.woo.Fetch('products', function (res) {
this.products = res.products;
console.log(this.products);
}.bind(this));
}
和page.html
<ion-col center text-center col-6 *ngFor="let product of products">
<ion-card>
<img src="{{product.featured_src}}" />
<ion-card-content>
<ion-card-title style="font-size: 100%">
{{ product.title | StripHTML }}
</ion-card-title>
</ion-card-content>
<ion-row center text-center>
<p style="color: red">
{{ product.price_html | StripHTML:{split:" ",index:0} }}
</p>
</ion-row>
</ion-card>
</ion-col>
问题:Fetch
加载并返回数据,但页面 View 没有刷新,直到我单击菜单切换按钮,然后页面重新呈现或刷新并且产品/数据显示...
当 Fetch
调用它重新渲染或刷新的 callback
函数时,是否可以做到这一点?
最佳答案
Angular 通常会检测变化并更新其 View 。它可能没有在 Woocommerce API 中获得更新。
尝试使用 ngZone
以确保 Angular 检测到更改。
import {NgZone} from '@angular/core'
constructor(ngZone: NgZone){}//inject in constructor
ionViewDidLoad() {
this.woo.Fetch('products', function (res) {
this.ngZone.run(()=>{//use here
this.products = res.products;
console.log(this.products);
});
}.bind(this));
}
关于javascript - 异步调用返回后重新加载页面内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228534/