Programatically change active tab on Ionic

That did the trick! Thanks!

Ohoooo, :smiley: Thanks allot I was looking only for this code. It helped me allot thank you very much

That worked for me! Really helpful

Worked for me - thanks a lot

Hi!
Thank you very much for your solution.
In my case I want to reload the page under the selected tab but this solutions does not reload the page.
Any suggestion please?

Thanks

No, you donā€™t. Thatā€™s not a meaningful concept in an Angular app, so there is something wrong with the way you are populating the data in your component. Change the properties of the component, and Angularā€™s change detection will update the DOM appropriately so it looks like a ā€œpage reloadā€ (but really isnā€™t).

@rapropos I want the constructor to be called again

Then you are doing things in the constructor that you should not be doing. Use lifecycle events such as ionViewWillEnter() instead.

Thank you very much! this was what I was looking for :slight_smile:

yes It will work 100%

I am having trouble getting this to work from the Three Dee touch pluginā€¦ the plugin fires correctly, but nothing happensā€¦

ngAfterViewInit() {
this.initScrollListener();
let outer = this;

this.threeDeeTouch.isAvailable().then(isAvailable => {
  console.log('### 3 d available: ' + isAvailable);
  if(isAvailable) {
    this.threeDeeTouch.watchForceTouches()
      .subscribe(
        (data: ThreeDeeTouchForceTouch) => {
          console.log('Force touch %' + data.force);
          console.log('Force touch timestamp: ' + data.timestamp);
          console.log('Force touch x: ' + data.x);
          console.log('Force touch y: ' + data.y);
        }
      );
    let actions: Array<ThreeDeeTouchQuickAction> = [{
        title: 'Home',
        iconType: 'home'
      }, {
        title: 'Policy',
        iconType: 'compose'
      }, {
        title: 'Mayors',
        iconType: 'home'
      }, {
        title: 'Meetings',
        iconType: 'book'
      }
    ];

    this.threeDeeTouch.configureQuickActions(actions);
    console.log('### config actions.');

    this.threeDeeTouch.onHomeIconPressed().subscribe(
     (payload) => {
      // returns an object that is the button you presed
      console.log('Pressed the: ' + JSON.stringify(payload));
      console.log('### title: ' + payload.title);

      if(payload.title == 'Mayors') {
        outer.navCtrl.parent.select(2); 
      } else if(payload.title == 'Home') {
        outer.navCtrl.parent.select(0); 
      } else if(payload.title == 'Policy') {
        outer.navCtrl.parent.select(3); 
      } else if(payload.title == 'Meetings') {
        outer.navCtrl.parent.select(1); 
      }
     }
    )

  }
});

}

The constructor is called only once when the component is being created. Use ionic lifecycle events for such here is ionic blog on thatt

Hey all, I did the suggested:

this.nav.getActiveChildNav().select(page.index);

then when running it I get in the console:

(getActiveChildNav) is deprecated and will be removed in the next major release. Use getActiveChildNavs instead.

What would be the up to date solution now? I canā€™t find documentation on what the select() method doesā€¦

Any help? Cheers!!

hey. I wrote a little bit down on this issue.
you can find it here:
https://www.one-second.me/tips-and-tricks/ionic-programmatically-change-active-tab

it works something like this:

const tabsNav = this.app.getNavByIdOrName('myTabsNav') as Tabs;
tabsNav.select(1);

---

<ion-tabs name="myTabsNav">
...
</ion-tabs>

have fun :wink:

6 Likes

marked, love it, work like a charm, any explanation on how this works?

hey.
sry i canā€™t follow. how does what work? you mean the code above?

Hey nvm I got it working the old method was deprecated

works like charm. Really helped a lot

Works like a charm!!! Marked like Thx!!

hi,

how to achieve this in ionic 1?
i tried $ionicTabsDelegate.select(1); but doesnt work.