结构指令 - 使用多个结构指令
优质
小牛编辑
132浏览
2023-12-01
@Component({
selector: 'app-root',
template: `
<div class="tabs-selection">
<tab
*ngFor="let tab of tabs; let i = index"
[active]="isSelected(i)"
(click)="setTab(i)">
{{ tab.title }}
</tab>
<div [ngSwitch]="tabNumber">
<template ngFor [ngForOf]="tabs" let-tab let-i="index">
<tab-content *ngSwitchCase="i">
{{tab.content}}
</tab-content>
</template>
<tab-content *ngSwitchDefault>Select a tab</tab-content>
</div>
`
})
export class AppComponent {
tabNumber: number = -1;
{ title: 'Tab 1', content: 'Tab content 1' },
{ title: 'Tab 2', content: 'Tab content 2' },
{ title: 'Tab 3', content: 'Tab content 3' },
];
setTab(num: number) {
this.tabNumber = num;
}
isSelected(num: number) {
return this.tabNumber === i;
}