Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can hide and show tabs conditionally in Ionics?

The case is the following: I have a ion-tabs container with several ion-tab elements, and different users that logs into my application. What I need to do is to show or hide the ion-tab elements depending on the user type logged.

I have tried to hide just one tab to check if it is possible and the only thing that have worked is:

<ion-tab title="..." icon="..." ui-sref="..." class="ng-hide">

The problem is that I need to do this dynamically, and if I use a directive like ng-show="false" or my own directive to add class="ng-hide", it does not work. Not even encapsulating the ion-tab inside a divand hide this div.

What am I doing wrong? Can somebody help me?

Thanks

like image 772
Timbergus Avatar asked Nov 03 '14 12:11

Timbergus


People also ask

How do you hide tabs in ionic?

There is a simple way to implement that - moving the route you wish to hide the tab-bar to the tabs module, outside of tabs children routes. Moving this route to tabs routing module will hide tab-bar from the page.

How do you hide tabs in ionic 3?

The function will execute when the page is loaded. If you want to hide the tabbar then do this line of code: tabs[key].

How do you change tabs in ionic?

All tabs can be changed by setting the value of tabsLayout on the <ion-tabs> element, or in your app's config. For example, this is useful if you want to show tabs with a title only on Android, but show icons and a title for iOS.

How do you use ionic tabs?

We can access the Ionic tabs by using the standard <ion-tabs> component. This component works as a router outlet to handle navigation. It does not provide any mechanism to switch between tabs. If you need to do switch between tabs, use <ion-tab-bar> as a direct child element of <ion-tabs>.


2 Answers

If you are already using class attribute on ion-tab, you can modify it as follows?...

<ion-tab title="..." icon="..." ui-sref="..." class="class1 class2 {{myFunctionName()}}">

And in your controller...

$scope.myFunctionName = function(){
    if () {
    // return "ng-show";
    } else {
    // return "ng-hide";
    }
}
like image 125
Manish Kr. Shukla Avatar answered Sep 28 '22 03:09

Manish Kr. Shukla


Very late to the party here but encountered this problem and found a more elegant solution (imho).

Using ng-show doesn't work on ion-tab (not sure why not) so instead use ng-if to call a function:

<ion-tab ng-if="showElement()">

Then in your controller:

$scope.showElement = function() {
    //logic to return a bool
}

I think this is more elegant because it means the element is never rendered rather than rendered and hidden.

like image 38
Kevin Coulson Avatar answered Sep 28 '22 02:09

Kevin Coulson