force view to reload in ionic2 framework

21,885

Solution 1

Ionic 1


I haven't used Ionic 2 but currently i m using Ionic 1.2 and if they are still using ui-router than you can use reload: true in ui-sref

or you can add below code to your logout controller

$state.transitionTo($state.current, $stateParams, {
    reload: true,
    inherit: false,
    notify: true
});

Angular 2


Use

$window.location.reload();

or

location.reload();

Solution 2

Found this answer here, (please note especially the line this.navCtrl.setRoot(this.navCtrl.getActive().component); which is by far the simplest solution that I've come across to reload present page for Ionic 2 & 3 and later versions of Angular (mine is 4), so credit due accordingly:

RELOAD CURRENT PAGE

import { Component } from '@angular/core';
import { NavController, ModalController} from 'ionic-angular';

@Component({
  selector: 'page-example',
  templateUrl: 'example.html'
})
export class ExamplePage {

  public someVar: any;

  constructor(public navCtrl: NavController, private modalCtrl: ModalController) {

  }

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

}

If you want to RELOAD A DIFFERENT PAGE please use the following (note this.navCtrl.setRoot(HomePage);:

import { Component } from '@angular/core';
import { NavController, ModalController} from 'ionic-angular';
import { HomePage } from'../home/home';

@Component({
  selector: 'page-example',
  templateUrl: 'example.html'
})
export class ExamplePage {

  public someVar: any;

  constructor(public navCtrl: NavController, private modalCtrl: ModalController) {

  }

  directToNewPage() {
    this.navCtrl.setRoot(HomePage);
  }

}
Share:
21,885

Related videos on Youtube

Basit
Author by

Basit

Updated on January 25, 2020

Comments

  • Basit
    Basit about 4 years

    After going through Clear History and Reload Page on Login/Logout Using Ionic Framework

    I want to know same question, but for ionic2 using typescript.

    On login and logout I need reload the app.ts, because there are classes that run libraries on construct.

    it would be basically redirect to home and reload.

  • Basit
    Basit about 8 years
    I have already posted link to answers of ionic1.. so this really does not help
  • Siddharth Pandey
    Siddharth Pandey about 8 years
    Ok Let me give it a try than will come back to you
  • Siddharth Pandey
    Siddharth Pandey about 8 years
    Bro are you using angular 2
  • Basit
    Basit about 8 years
    Yes ionic2 comes with angular2
  • Basit
    Basit about 8 years
    do you want to write that in answer, so I can accept it as answer
  • Siddharth Pandey
    Siddharth Pandey about 8 years
  • Jorge Luis Jiménez
    Jorge Luis Jiménez about 7 years
    That does not work for Ionic 2, it is different in the new version.
  • Yokesh Varadhan
    Yokesh Varadhan over 6 years
    do we need to add import for ionic2 @paulitto
  • paulitto
    paulitto over 6 years
    @YokeshVaradhan yes you'll need to import it from ionic-angular
  • user2828442
    user2828442 over 5 years
    And if i want to refresh a part of page, then how can i do that ?

Related