2017-04-03 90 views
0

我想知道是否有辦法刷新ngOnInit()函數或刷新組件。我不想用拉來刷新方法。我想按鈕點擊或點擊。謝謝如何刷新頁面或刷新ngOnInit()離子2

home.ts文件

checkNetwork() { 
    console.log("check internet"); 
this.platform.ready().then(() => { 
    if(this.network.type == "none"){ 
    this.shouldHide = false; 
    this.dividerHide = true; 
    } 
    let alert = this.alertCtrl.create({ 
     title: "Connection Status", 
     subTitle: <string> this.network.type, 
     buttons: ["OK"] 
    }); 
    alert.present(); 
}); 
} 

home.html的文件

<ion-card [hidden]="shouldHide"> 
<ion-card-header> 
    <img src="img/sad.png" /> 
</ion-card-header> 
<ion-card-content> 
    <ion-card-title style="text-align:center"> 
    No INTERNET! 
    </ion-card-title> 
    <button ion-button full (click)="refreshPage($event)">Retry</button> 
</ion-card-content> 

當連接將可我想頁面刷新

+0

這麼叫上按一下按鈕加載數據功能..不知道什麼是這裏的問題 –

+0

我;使用離子加載插件的功能從服務器下載數據,但有時數據下載失敗,但微調持續。我想要一個可以重新啓動頁面的函數,以便函數和加載模塊 –

+0

可以共享相關代碼並編輯問題? –

回答

2

謝謝大家,這是我如何管理它

home.ts文件

ngOnInit(){ 
this.LoadData();} 


checkNetwork() { 
    console.log("check internet"); 
this.platform.ready().then(() => { 
    if(this.network.type == "none"){ 
    this.shouldHide = false; 
    this.dividerHide = true; 
    }else{ 
    this.shouldHide = true; 
    this.dividerHide = false; 
    this.presentLoading() 
    } 
}); 
} 

refreshPage() 
    {this.LoadData(); 
} 

public LoadData(){...} 

Home.html文件中

<ion-card [hidden]="shouldHide"> 
<ion-card-header> 
    <img src="img/sad.png" /> 
</ion-card-header> 
<ion-card-content> 
    <ion-card-title style="text-align:center"> 
    Pas de connexion internet! 
    </ion-card-title> 
    <button ion-button full (click)="refreshPage()">Reessayer</button> 
</ion-card-content> 

-2

您可能需要重構你的代碼結構,因此它不需要頁面刷新,使用init方法o R Rx的訂閱。

如果您需要重新加載頁面,爲什麼不使用重新加載方法?

window.location.reload();

編輯:

是否使用network.onConnect訂閱?

// watch network for a connection 
 
let connectSubscription = this.network.onConnect().subscribe(() => { 
 
    console.log('network connected!');
 
 
    // We just got a connection but we need to wait briefly 
 
    // before we determine the connection type. Might need to wait
 
 
    // prior to doing any api requests as well. 
 
    setTimeout(() => { 
 
    if (this.network.type === 'wifi') { 
 
     console.log('we got a wifi connection, woohoo!'); 
 
    } 
 
    }, 3000); 
 
});

+0

做到了在離子2上工作?它會重新加載整個應用程序或只有組件? –

+0

是的,它會工作,因爲ionic2應用程序正在鉻瀏覽器下運行 – karser

+0

這是一個非常糟糕的做法,因爲整個應用程序將被重新加載。任何會話數據也將丟失。 – JoeriShoeby

0

使用此代碼

refreshPage() { 
    this.navCtrl.setRoot(this.navCtrl.getActive().component); 
}