Bee*_*ice 8 templates if-statement angular-template angular
Angular的NgTemplateOutlet允许您将上下文传递给出口以进行属性绑定.
<ng-container *ngTemplateOutlet="eng; context: {$implicit: 'World'}"></ng-container>
<ng-template #eng let-name><span>Hello {{name}}!</span></ng-template>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
如何将上下文传递给*ngIf语法中引用的这些模板?
实际上,您可以将条件输入到ngTemplateOutlet中(并摆脱ngIf)。
<ng-container *ngTemplateOutlet="condition ? template1 : template2; context: {$implicit: 'World'}">
</ng-container>
Run Code Online (Sandbox Code Playgroud)