我已经构建了一个ul
并且只想在第一个li
元素上设置类.
我想class="active"
在第一个李上设置唯一的.我确实得到了class属性的索引,但这不是我想要的.
import { Component, View, NgFor,Inject,forwardRef,Input, NgIf, FORM_DIRECTIVES } from 'angular2/angular2';
@Component({
selector: 'tabs'
})
@View({
template: `
<ul>
<li *ng-for="#tab of tabs;#index = index" class="{{index}}" (click)="selectTab(tab)">{{tab.tabTitle}}</li>
</ul>
<ng-content></ng-content>
`,
directives: [NgFor]
})
export class Tabs {
get tabs() {
return this._tabs;
}
set tabs(value) {
this._tabs = value;
}
private _tabs;
constructor() {
console.log("ctor.Tabs");
this._tabs = [];
}
selectTab(tab) {
this._tabs.forEach((tab) => {
tab.active = false;
});
tab.active = true;
}
addTab(tab: Tab) {
if (this._tabs.length === 0) {
tab.active = true;
}
else {
tab.active = false;
}
this._tabs.push(tab);
}
}
@Component({
selector: 'tab',
properties: ['tabTitle: tab-title']
})
@View({
template: `
<div [hidden]="!active" [class]="active">
<ng-content/>
</div>
`
})
export class Tab {
@Input() index: number;
constructor(@Inject(forwardRef(() => Tabs)) tabs: Tabs) {
console.log("ctor.Tab") ;
tabs.addTab(this);
console.log(tabs);
}
get active() {
return this._active;
}
set active(value) {
this._active = value;
}
private _active;
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*nez 40
按照@jeff的要求
您只需使用此行即可实现
<li *ngFor="let tab of tabs; let index = index" [class.active]="index == 0" ...>
Run Code Online (Sandbox Code Playgroud)
很高兴它有帮助:)
更新
使用beta 15,first
添加了局部变量,因此可以将原始解决方案重写为
<li *ngFor="let tab of tabs; let isFirst = first" [class.active]="isFirst" ...>
Run Code Online (Sandbox Code Playgroud)
请参阅Angular 2 - ngFor - 局部变量"first"不起作用
归档时间: |
|
查看次数: |
34512 次 |
最近记录: |