angular - 带有 *ngIf else 模板的 ng-container 不适用于 ContentChildren QueryList
问题描述
我正在尝试通过@ContentChildren
和 QueryList
ussign动态更改组件的内容ng-container *ngIf="condition; else tplRef"
,当condition
真正的ng-container
内容是可见的,QueryList
但当condition
是假的时候,ng-template
内容是不可见的。
我的目标是根据“查询”可见的条件显示不同的元素
解决方案
你需要保持<ng-template #ref>... </ng-template>
在<app-container>
试试这样:
<app-container>
<app-container-item name="Item 1"></app-container-item>
<app-container-item name="Item 2"></app-container-item>
<!-- work fine if true -->
<ng-container *ngIf="true; else ref">
<app-container-item name="Item when true"></app-container-item>
</ng-container>
<!-- should display content from ref, but don't work :( -->
<ng-container *ngIf="false; else ref">
<app-container-item name="Item when true"></app-container-item>
</ng-container>
<ng-template #ref>
<app-container-item name="Item when false"></app-container-item>
</ng-template>
</app-container>
推荐阅读
- javascript - Ejs页面未在简化版代码上呈现
- java - 并发执行事务时出现 HibernateException
- jquery - 如何在没有 Async:false Jquery 的 Each 循环中调用 Ajax
- shell - 为 spark-submit 创建通用 shell 脚本
- javascript - 如何在本地测试通过 Javascript 发送表单数据?
- python-3.x - IDLE 3.8.4 和 3.9.0b4 不会保存非 ascii 字符的文件
- python-3.x - 在 Discord 语音通道上播放音频文件 wile 文件正在生成
- javascript - 如何根据javascript中的某些标志获取特定对象属性数组的总和?
- java - 在 RxJava 中处理 PublishSubject 对象 内存泄漏
- amazon-web-services - 将 GCP 对象存储数据迁移到 AWS S3 存储桶