Search code examples
angularionic2refreshpage-refreshbuttonclick

How to refresh a page or refresh ngOnInit() in ionic 2


i'm wondering if there is a way to refresh the ngOnInit() function or refresh the component. i don't wanna use the pull to refresh method. i want to do it on button click or tap. Thank you

home.ts file

  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 file

<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>

when the connection will be available i want the page to refresh


Solution

  • Thank you all, this is how i managed it

    home.ts file

    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 file

    <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>