首页 文章

如何从* ngIf将上下文传递给Angular 5模板

提问于
浏览
5

Angular的NgTemplateOutlet允许您将上下文传递给出口以进行属性绑定 .

<ng-container *ngTemplateOutlet="eng; context: {$implicit: 'World'}"></ng-container>
<ng-template #eng let-name><span>Hello {{name}}!</span></ng-template>

Angular的 *ngIf 允许您根据布尔条件嵌入一个或另一个模板:

<ng-container *ngIf="isConditionTrue; then one else two"></ng-container>
<ng-template #one>This shows when condition is true</ng-template>
<ng-template #two>This shows when condition is false</ng-template>

如何将上下文传递给 *ngIf 语法中引用的这些模板?

2 回答

  • -3

    为什么不这样做:

    <ng-container *ngIf="isConditionTrue; else two">
     // your first template, #one, goes here.
    </ng-container>
    
    <ng-template #two>
     // your second template, #two goes here.
    </ng-template>
    
  • 4

    实际上你可以输入你的条件到ngTemplateOutlet(并摆脱ngIf) .

    <ng-container *ngTemplateOutlet="condition ? template1 : template2; context: {$implicit: 'World'}">
    </ng-container>
    

相关问题