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语法中引用的这些模板?
解决方法
实际上你可以输入你的条件到ngTemplateOutlet(并摆脱ngIf).
<ng-container *ngTemplateOutlet="condition ? template1 : template2; context: {$implicit: 'World'}"> </ng-container>