加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:23:27 所属栏目:安全 来源:网络整理
导读:我需要将tappable指令添加到自定义组件内的离子卡组件.我使用@Input()myInputBool,类似于: ng-container *ngIf="myInputBool" ion-card tappable ng-container render="myContent"/ng-container /ion-card/ng-containerng-container *ngIf="!myInputBool" i
我需要将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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读