Skip to content

Instantly share code, notes, and snippets.

@Bengejd
Last active October 31, 2018 03:36
Show Gist options
  • Save Bengejd/65bbfd487cde8cfd07b21e594f856004 to your computer and use it in GitHub Desktop.
Save Bengejd/65bbfd487cde8cfd07b21e594f856004 to your computer and use it in GitHub Desktop.
Ionic 3 Show/Hide TabBar "directive" to programmatically hide/show tabs using this.tabs.show()/this.tabs.hide().
import {Directive} from '@angular/core';
/*
* This is technically a directive
* but we have to treat it like a provider to be able to manipulate it directly.
* So it must be imported into our app.module as a provider, instead of a directive.
*
* The reason I'm keeping it as a directive, instead of a service provider, is because it interacts with the DOM
* and not any data retrial / presentation, even though it's imported as one...weird I know.
*
* We inline the tabBarElement:any = document.querySelector('.tabbar.show-tabbar'); because sometimes
* Angular/Ionic runs into issues trying to access the document on page transitions. This was the best option I found.
*/
@Directive({})
export class TabBarDirective {
/*
* Shows the tabs.
*/
public show() {
let tabBarElement:any = document.querySelector('.tabbar.show-tabbar');
tabBarElement.style.display = 'flex';
}
/*
* Hides the tabs.
*/
public hide() {
let tabBarElement:any = document.querySelector('.tabbar.show-tabbar');
tabBarElement.style.display = 'none';
}
}
import {TabBarDirective} from '../../directives/tab-bar.directive';
import {IonicPage} from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html',
})
export class HomePage {
constructor(private tabs: TabBarDirective){
}
ionViewWillEnter(){
// this.tabs.hide();
// or
// this.tabs.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment