using ng-content in ng-template

后端 未结 2 1770
醉话见心
醉话见心 2020-12-30 02:17

Is it possible to use (and its select option) inside a or does it only works within a component ?

相关标签:
2条回答
  • 2020-12-30 02:56

    You can use ng-content inside ng-template.

    This is something I put together a while back demonstrating putting the ng-content somewhere on dom based on property. look at the html of tabs.component to see this in use.

    https://stackblitz.com/edit/mobile-ng-content

    0 讨论(0)
  • 2020-12-30 02:58

    As far as i know it is not possible using ng-content, but you can provide parameters to the template. So it's possible to pass another NgTemplate, which can again be used with an NgTemplateOutlet inside the original template. Here's a working example:

    <ng-container *ngTemplateOutlet="tpl, context: {$implicit: paramTemplate}">
    </ng-container>
    
    <ng-template #paramTemplate>
      <span>Hello</span>
    </ng-template>
    
    
    <ng-template #tpl let-param>
      <ng-container *ngTemplateOutlet="param"></ng-container> World !
    </ng-template>
    

    Actually it is even possible to pass multiple templates to the original template:

    <ng-container *ngTemplateOutlet="tpl, context: {'param1': paramTemplate1, 'param2': paramTemplate2}">
    </ng-container>
    
    <ng-template #paramTemplate1>
      <span>Hello</span>
    </ng-template>
    
    <ng-template #paramTemplate2>
      <span>World</span>
    </ng-template>
    
    
    <ng-template #tpl let-param1="param1" let-param2="param2">
      <ng-container *ngTemplateOutlet="param1"></ng-container>
      <ng-container *ngTemplateOutlet="param2"></ng-container>
    </ng-template>
    
    0 讨论(0)
提交回复
热议问题