Created
June 14, 2018 10:09
-
-
Save rajatgeekyants/b901abe019a3afa19149aae10c59f146 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Component, | |
ContentChildren, | |
QueryList, | |
AfterContentInit, | |
} from '@angular/core'; | |
import {TabComponent} from './tab.component'; | |
@Component({ | |
selector: 'ngx-tabs', | |
template: ` | |
<ul class="nav nav-tabs"> | |
<li *ngFor="let tab of tabs" (click)="selectTab(tab)" [class.active]="tab.active"> | |
<a href="#">{{tab.tabTitle}}</a> | |
</li> | |
</ul> | |
<ng-content></ng-content> | |
`, | |
}) | |
export class TabsComponent implements AfterContentInit { | |
@ContentChildren(TabComponent) tabs: QueryList<TabComponent>; | |
ngAfterContentInit() { | |
const activeTabs = this.tabs.filter(tab => tab.active); | |
if (activeTabs.length === 0) { | |
this.selectTab(this.tabs.first); | |
} | |
} | |
selectTab(tab: TabComponent) { | |
this.tabs.toArray().forEach(tab => (tab.active = false)); | |
tab.active = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment