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

标签 angular ionic3

我需要将 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/

相关文章:

javascript - ionic 3 选项卡中的 subview 在查看子组件时显示两个标题

javascript - Ionic 2/3 - 如何在屏幕较宽时查看 2 张幻灯片?

css - 不向右移动

angular - typescript 导入错误的 Angular

typescript - 在angular2中调用typescript getter

node.js - Domino 10 AppDevPack本地安装问题

ios - (iOS) 从非 ascii 键捕获事件

angular - Typescript:如何使用枚举来强制数组仅包含该枚举中的特定值?

angular - 用 takeUntil 取消 observable 不起作用

typescript - 存储不返回字符串 ionic