Search code examples
angularionic2

Ionic2 - check if page is active


How can I check if a ionic2 page is active?

For example I want to hide a button if a page is active:

<button primary [hidden]="isActive('actualPageName')">
     Should be hidden
</button>

Solution

  • IONIC 2 & 3: Because of the uglification you cannot use pagename for production.

    I check the active page by looking if the last page on the controller stack is of the instance of the page:

    import { NavController } from 'ionic-angular';
    import { myPage } from '../../pages/my/my';
    ...
    constructor(public navCtrl: NavController) {}
    ...
    let active = this.navCtrl.last().instance instanceof MyPage;
    

    IONIC 4: Routing is changed. This looks like the new way:

    import { Router } from '@angular/router';
    ...
    constructor(public router: Router) {}
    ...
    let active =  this.router.isActive('mypage', false)