Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select tabs in ionic 2?

Tags:

ionic2

I'm struggling to create a basic application in Ionic2 that uses both the side-menu and the tabs navigation. I understand the concepts of the navigation stack and that each tab has its own navigation stack, but I can't grasp the control over the tabs themselves.

The tabs starter template initializes a project with one ion-nav having its rootpage pointing to "rootPage", a property of the @App pointing to a TabsPage class.

<ion-nav id="nav" [root]="rootPage" #content></ion-nav>

The TabsPage class defines 3 properties, one for each page, pointing to their respective classes (each class decorated with @Page). But the TabsPage class itself doesn't seem to have any function, or be injected with a tabs controller and I find little to no documentation on how to acquire a Tabs instance (there are instance methods referenced on http://ionicframework.com/docs/v2/api/components/tabs/Tabs/)

What I managed to do: Use one tab to control the other.

import {Page, Tabs} from 'ionic-angular';

@Page({
    templateUrl: 'build/pages/timeline/timeline.html'
})
export class Timeline {
    tabs:Tabs;
    constructor(tabs:Tabs) {
        this.tabs=tabs;
        this.selectTab(2);
    }
    selectTab(i:number) {
        this.tabs.select(i);
    }
}

The page above is injected with a Tabs instance, which inherits from NavController. The Tabs instance has the desired select method, and I can point to a different tab (by index, not by name). So in this situation selecting my 'timeline' tab will trigger the constructor, and instead of going to the timeline tab we end up selecting the 2nd tab.

What I would like to do: navigate to a tab with a link in the side-menu. My side-menu consists of two ion-items, simple buttons with a click listener. In Ionic 1.x I could use a ui-sref or a href to match a certain state, but in Ionic 2 I can't figure out how to control my tabs. I can access the ion-nav by giving it an id and using app.getComponent('nav'), but I can not target the ion-tabs this way (hoping it would be bound to a Tabs controller instance).

like image 764
Thomas Withaar Avatar asked Apr 07 '16 15:04

Thomas Withaar


People also ask

How do you use ionic ion tabs?

Activating TabsEach ion-tab-button will activate one of the tabs when pressed. In order to link the ion-tab-button to the ion-tab container, a matching tab property should be set on each component. The ion-tab-button and ion-tab above are linked by the common tab property.

How do you add tabs to the ionic app?

Adding Tabs Bar in Ionic Application The tab navigation is created by adding the ion-tabs having ion-tab-bar inside it which creates a tab bar on position defined in the slot property. Each tab button is created using the ion-tab-button with tab property which is the path of tab page resulting navigation.

How do I turn off ionic tabs?

You can set the disabled property on ion-tab-button : https://ionicframework.com/docs/api/tab-button#properties. and then set isButtonDisabled to true or false.


1 Answers

Each ion-tab is a declarative component for a NavController. Basically, each tab is a NavController. For more information on using navigation controllers take a look at the NavController API Docs.

So to access the array of tabs from inside a specific tab page (component) we can set our target path with as simple as :

NavController.parent 

Now suppose, we are in a child page of one of our tabs - the component class will be somewhat similar as below:

import { Component } from '@angular/core';
import { NavController, Nav , Tabs } from 'ionic-angular';

// import Tabs

import { Page2} from '../page-2/page-2';
import { Page3} from '../page-3/page-3';

@Component({
  templateUrl: 'build/pages/page-1/page1.html'
})
export class Page1 {
  tab:Tabs;

  // create a class variable to store the reference of the tabs


  constructor(public navCtrl: NavController, private nav: Nav) {
    this.tab = this.navCtrl.parent;

    /*Since Tabs are declarative component of the NavController 
      - it is accessible from within a child component. 
      this.tab - actually stores an array of all the tabs defined 
      in the tab.html / tab component.
   */
  }

  goToTab2 (){  
    this.tab.select(1);

  //  the above line is self explanatory. We are just calling the select() method of the tab
  }
  goToTab3 (){
    this.tab.select(2);
  }
} 

Hope this helps.

like image 158
Arup Bhattacharya Avatar answered Oct 16 '22 10:10

Arup Bhattacharya