Access selected tab id from child view, after loading


#1

Hi, I’m making an app with tab navigation, following the default template. I need to access the selected tab id, because I have similar “homepage” in every tab. By accessing this value I could use the same template for the 3 tabs and change the only different parts using this value and data binding. I’m checking for the tab’s selected id on the event “ionViewDidEnter()” of the child. By the way, after some testing, I can say for sure that the selected ID value is changed AFTER the “ionViewDidEnter()” event is fired.
What’s a possible solution to the problem?
Thanks for the help!


#2

Just found the solution. Posting here hoping to help someone with the same problem. I simply subscribed to the ionChange event of the parent tab, choosing the custom text for every homepage after it fires

ionViewDidEnter() {

      this.navCtrl.parent.ionChange.subscribe(() => {
        this.tabID = this.navCtrl.parent.getSelected().id;
        if(this.tabID=="t0-0"){
          this.customText = "<h1>testo</h1>";
        }
        if(this.tabID=="t0-1"){
          this.customText = "<h1>voce</h1>";
        } 
        if(this.tabID=="t0-2"){
          this.customText = "<h1>immagine</h1>";
        }
    })
}

Yeah I know I should use switch/case, this is just temporary code to test the situation