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

前端之家收集整理的这篇文章主要介绍了angular – 如何在没有* ngIf的情况下渲染ng-container或ng-template?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要将tappable指令添加自定义组件内的离子卡组件.我使用@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>

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

<ng-container *ngIf="thisVariableDoesNotExist else myContent"> </ng-container>

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

解决方法

我会使用ngTemplateOutlet而不是render选项:

<ng-container *ngTemplateOutlet="myContent"></ng-container>

也可以看看

> https://angular.io/api/common/NgTemplateOutlet#how-to-use

猜你在找的Angularjs相关文章