如何在没有*ngIf的情况下渲染ng-container或ng-template?

dis*_*nte 7 ionic3 angular

我需要将tappable指令添加到ion-card自定义组件内的组件.我用的@Input() myInputBool是:

<ng-container *ngIf="myInputBool">
    <ion-card tappable>
        <ng-container render="myContent"></ng-container>
    </ion-card>
</ng-container>

<ng-container *ngIf="!myInputBool">
    <ion-card tappable>
        <ng-container render="myContent"></ng-container>
    </ion-card>
</ng-container>

<ng-container #myContent>
    This is my content
</ng-container>
Run Code Online (Sandbox Code Playgroud)

当然它不起作用,因为没有"渲染"选项.到目前为止,我的解决方法是在ng-container中添加一个不存在的变量

<ng-container *ngIf="thisVariableDoesNotExist else myContent"> </ng-container>
Run Code Online (Sandbox Code Playgroud)

但它感觉很糟糕和黑客.有更好的方法吗?

yur*_*zui 13

我会用ngTemplateOutlet而不是render选项:

<ng-container *ngTemplateOutlet="myContent"></ng-container>
Run Code Online (Sandbox Code Playgroud)

也可以看看