2017-04-05 126 views
3

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做負載和返回數據,但頁面視圖不被刷新,直到我點擊菜單切換按鈕,然後在頁面重新渲染或者刷新和產品/數據顯示...

是否有消息使Fetch調用callback函數它重新顯示或刷新?

+0

你是說這個頁面沒有顯示你的返回數據嗎? –

+0

@suraj是啊,它不顯示返回的數據 – Abanoub

回答

1

Angular通常會檢測更改並更新其視圖。它可能沒有得到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)); 
     } 
+0

hummmm是啊我相信工作!謝謝... – Abanoub

+0

好..很高興它解決了..有一點與您的評論混淆.. –

+0

哈哈哈對不起'鉻'行爲怪異xD – Abanoub