Skip to content

Instantly share code, notes, and snippets.

@kyr0
Created June 26, 2017 18:44
Show Gist options
  • Save kyr0/11830a1560a6d1173be84b966d171edc to your computer and use it in GitHub Desktop.
Save kyr0/11830a1560a6d1173be84b966d171edc to your computer and use it in GitHub Desktop.
Ionic 3 TabHiddenDirective to programmatically hide tabs using [tab-hidden]="true"; it is adaptive and also works fine on view changes
import {Directive, ElementRef, Input} from '@angular/core';
@Directive({ selector: '[tab-hidden]' })
export class TabHiddenDirective {
@Input('tab-hidden')
tabHidden: boolean;
private _tabElCache: Map<string, Element> = new Map();
constructor(private _el: ElementRef) {}
ngAfterViewChecked() {
let tabId = this._el.nativeElement.id.split('-');
tabId.shift();
tabId.unshift('tab');
tabId = tabId.join('-');
let tabEl: HTMLAnchorElement;
if (!this._tabElCache.has(tabId)) {
tabEl = <HTMLAnchorElement> document.querySelector('#' + tabId);
this._tabElCache.set(tabId, tabEl);
} else {
tabEl = <HTMLAnchorElement> this._tabElCache.get(tabId);
}
if (tabEl) {
if (this.tabHidden) {
tabEl.style.display = 'none';
} else {
tabEl.style.display = 'flex';
}
}
}
}
@licalac
Copy link

licalac commented Feb 14, 2018

Many thanks, it works perfectly for me ;-)

@fabiansch
Copy link

thanks a lot 👍

..but do you know why the hidden directive mentioned here does not work? Did it only worked before ionic 3?

@umutsenoglu
Copy link

Many thanks, Perfect and clear.

@negar-mir
Copy link

Thanks for creating this awesome directive.
I used this directive. it is perfect when I create apk using "ionic cordova run android" but when I run "ionic cordova run android --prod",
I face the following error:
Can't bind to 'tab-hidden' since it isn't a known property of 'ion-tab'. 1. If 'ion-tab' is an Angular
component and it has 'tab-hidden' input, then verify that it is part of this module. 2. If 'ion-tab' is a
Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress
this message. 3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.
(" <ion-tab [ERROR ->][tab-hidden]="false" tabTitle="Main" [root]="deliveryTabsMainPage"
[rootParams]="navParams.data"></io")

How can I solve it?
Please help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment