ng-content in for loop

前端 未结 1 1505
无人及你
无人及你 2021-01-01 06:32

I\'m trying to create a tab component using bootstrap.


  Description 1
  

        
相关标签:
1条回答
  • 2021-01-01 06:57

    You can take a look at how angular material implemented tabs control.

    There are some caveats about this approach https://github.com/angular/angular/issues/18691 but anyway here is simplified version:

    tab.component.ts

    @Component({
      selector: 'app-tab',
      template: `
        <ng-template>
          <ng-content></ng-content>
        </ng-template>`
    })
    export class TabComponent {
      ...
    
      @ViewChild(TemplateRef) template: TemplateRef<any>;
    }
    

    tabs.component.ts

    <div *ngFor="let tab of tabs; let i = index" 
               class="tab-pane" [ngClass]="{'active': i === 0}"...>
       <ng-container *ngTemplateOutlet="tab.template"></ng-container>
    </div>
    

    Plunker Example

    0 讨论(0)
提交回复
热议问题