Created
December 21, 2016 19:47
-
-
Save wKoza/9493da3c83696bacf96f4f714ce4252a 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/tab.component"; | |
@Component({ | |
selector: 'tabs', | |
template:` | |
<ul class="nav nav-tabs"> | |
<li *ngFor="let tab of tabs" (click)="selectTab(tab)" [class.active]="tab.active"> | |
<a href="#">{{tab.title}}</a> | |
</li> | |
</ul> | |
<ng-content></ng-content> | |
` | |
}) | |
export class TabsComponent implements AfterContentInit { | |
@ContentChildren(TabComponent) tabs: QueryList<TabComponent>; | |
// contentChildren sont maintenant settés | |
ngAfterContentInit() { | |
// recupère les tabs actifs | |
let activeTabs = this.tabs.filter((tab)=>tab.active); | |
// Activer le premier s'il n'en existe pas | |
if(activeTabs.length === 0) { | |
this.selectTab(this.tabs.first); | |
} | |
} | |
selectTab(tab: TabComponent){ | |
// desactive tous les tabs | |
this.tabs.toArray().forEach(tab => tab.active = false); | |
// Active le tab sur lequel l'utilisateur à cliqué | |
tab.active = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment