结构指令 - 使用多个结构指令

优质
小牛编辑
128浏览
2023-12-01
  1. @Component({
  2. selector: 'app-root',
  3. template: `
  4. <div class="tabs-selection">
  5. <tab
  6. *ngFor="let tab of tabs; let i = index"
  7. [active]="isSelected(i)"
  8. (click)="setTab(i)">
  9. {{ tab.title }}
  10. </tab>
  11. <div [ngSwitch]="tabNumber">
  12. <template ngFor [ngForOf]="tabs" let-tab let-i="index">
  13. <tab-content *ngSwitchCase="i">
  14. {{tab.content}}
  15. </tab-content>
  16. </template>
  17. <tab-content *ngSwitchDefault>Select a tab</tab-content>
  18. </div>
  19. `
  20. })
  21. export class AppComponent {
  22. tabNumber: number = -1;
  23. { title: 'Tab 1', content: 'Tab content 1' },
  24. { title: 'Tab 2', content: 'Tab content 2' },
  25. { title: 'Tab 3', content: 'Tab content 3' },
  26. ];
  27. setTab(num: number) {
  28. this.tabNumber = num;
  29. }
  30. isSelected(num: number) {
  31. return this.tabNumber === i;
  32. }