我要實現我的網頁上的標籤,因此通過本教程Angular2:執行標籤
https://dzone.com/articles/learning-angular-2-creating-a-tabs-component但不知何故,它不適合我Plunker工作去了: http://plnkr.co/edit/jwBB7jd8KDXmndGBz4ZN?p=preview
import { Component, OnInit,Input } from '@angular/core';
@Component({
selector: 'tab',
template: '<div [hidden]="!active" class="pane">
<ng-content></ng-content>
</div>',
styleUrls: ['./tab.component.css']
})
export class Tab{
@Input('tabTitle') title: string;
@Input() active = false;
}
和標籤組件:
import { Component, OnInit,ContentChildren,QueryList ,AfterContentInit} from '@angular/core';
import { Tab } from './tab.component.ts';
@Component({
selector: 'tabs',
template: `<ul class="nav nav-tabs">
<li *ngFor="let tab of tabs" [hidden]='true' (click)="selectTab(tab)" [class.active]="tab.active">
<a>{{tab.title}}</a>
</li>
</ul>
<ng-content></ng-content>`,
styleUrls: ['./tabs.component.css']
})
export class Tabs implements AfterContentInit{
@ContentChildren(Tab) tabs: QueryList<Tab>;
ngAfterContentInit() {
let activeTabs = this.tabs.filter((tab)=>tab.active);
if(activeTabs.length === 0) {
this.selectTab(this.tabs.first);
}
}
selectTab(tab: Tab){
this.tabs.toArray().forEach(tab => tab.active = false);
tab.active = true;
}
}
請注意:這是我第一次與Plunker合作,請原諒我。 我希望有人能幫助我。
謝謝。
改進Plunker http://plnkr.co/edit/Mdj9llvGiEyMBmbSRoo7?p=preview但它拋出一個錯誤約'區不defined'(也是一個新的Angular2 TS Plunker模板) –