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

前端之家收集整理的这篇文章主要介绍了如何从* ngIf将上下文传递给Angular 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语法中引用的这些模板?

解决方法

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

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

猜你在找的Angularjs相关文章