ionic2

How to get current page from Navigation in ionic 2


I am new to Ionic2, and I am trying to build dynamic tabs based on current menu selection. I am just wondering how can I get current page using navigation controller.

...
export class TabsPage {
  constructor(navParams: NavParams,navCtrl:NavController) {
    //here I want to get current page
 }
}
...

From api documentation I feel getActiveChildNav() or getActive() will give me the current page, but I have no knowledge on ViewController/Nav.

Any help will be appreciated. Thanks in advance.


Solution

  • Full example:

    import { NavController } from 'ionic-angular';
    
    export class Page {
    
      constructor(public navCtrl:NavController) {
      }
    
      (...)
    
      getActivePage(): string {
        return this.navCtrl.getActive().name;
      }
    }
    

    Method to get current page name:

    this.navCtrl.getActive().name
    

    More details here