我需要将 tappable
指令添加到自定义组件内的 ion-card
组件。我使用一个 @Input() myInputBool
,类似于:
<ng-container *ngIf="myInputBool">
<ion-card>
<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>
另见
关于angular - 如何在没有 *ngIf 的情况下渲染 ng-container 或 ng-template?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433350/