【发布时间】:2016-11-17 13:49:07
【问题描述】:
我想在我的页面上实现标签,因此完成了本教程
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 plnkr.co/edit/Mdj9llvGiEyMBmbSRoo7?p=preview 但它会引发关于
Zone is not defined的错误(也在新的 Angular2 TS Plunker 模板中)