14
我想在angularjs 2模板中使用enum。 下面是我的代碼如何在Angular 2模板中使用enum
@Component({
selector: 'test',
template: `
<ul class="nav navbar-nav">
<li class="{{activeSection == SectionType.Primary ? 'active': ''}}"><a href="javscript:void(0);" (click)="setActiveSection(SectionType.Primary)">Primary Details</a></li>
<li class="{{activeSection == SectionType.Aditional ? 'active': ''}}"><a href="javscript:void(0);" (click)="setActiveSection(SectionType.Aditional)">Additional Details</a></li>
<li class="{{activeSection == SectionType.Payment ? 'active': ''}}"><a href="javscript:void(0);" (click)="setActiveSection(SectionType.Payment)">Payment Settings </a></li>
</ul>`
})
export class TestComponent{
activeSection: SectionType = SectionType.Primary;
setActiveSection(section: SectionType) {
this.activeSection = section;
}
}
這裏是我的枚舉:
enum SectionType {
Primary,
Aditional,
Payment
}
它引發異常:類型錯誤:無法讀取未定義
第一解決方案可能工作,但怎麼樣'(點擊)=「setActiveSection(SectionType.Primary)」' 和第二個解決方案 'SectionType:SectionType = SectionType;'給出錯誤 Type'Typeof SectionType'不可分配給'SectionType' –
試試這個:'SectionType = SectionType;' –
我使用'SectionType:any = SectionType;'。看到這個plunkr:http://plnkr.co/edit/Mos2zocjWxiYx5rnY4PI?p=preview。 –