Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current page from Navigation in ionic 2

Tags:

ionic2

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.

like image 284
amyst Avatar asked Nov 22 '16 17:11

amyst


People also ask

How do you navigate pages in ionic?

Navigation in Ionic works like a simple stack, where we push new pages onto the top of the stack, which takes us forwards in the app and shows a back button. To go backwards, we pop the top page off.

What is NavController in Ionic?

NavController is the base class for navigation controller components like Nav and Tab . You use navigation controllers to navigate to pages in your app. At a basic level, a navigation controller is an array of pages representing a particular history (of a Tab for example).


2 Answers

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

like image 102
Rafael Motta Avatar answered Oct 12 '22 02:10

Rafael Motta


OMG! This Really Helped mate, Tons of Thanks! @Deivide I have been stuck for 1 Month, Your answer saved me. :) Thanks!

if(navCtrl.getActive().component === DashboardPage){
    this.showAlert();
}
else
{
    this.navCtrl.pop();    
}
like image 29
Aditya Kumar Avatar answered Oct 12 '22 01:10

Aditya Kumar