angular - 在 ngAfterViewInit 之后 createEmbeddedView 进入内容投影
问题描述
我有一个设置,parent.component
其中一些复杂的动态模板在一些命名ng-template
元素中解析。然后我想在页面上以动态顺序插入这些,我在ngAfterViewInit
using中执行此操作createEmbeddedView
。这需要一个setTimeout
,否则我会得到一个 Angular ExpressionChangedAfterItHasBeenCheckedError
。
这本身看起来很混乱,但现在我正在尝试将这些动态元素内容投影到子组件中。元素不出现。我应该如何使用 Angular 的生命周期和更改检测才能正确执行此操作?
另一个会使示例复杂化的怪癖:在我的实际实现中,子组件的ng-content
位置隐藏在ngIf
指令后面。如果将投影的动态内容初始化为 on,则不会出现,但如果将其从 off 切换为 on,则会出现,因此子组件正在以某种方式接收和保存投影。
例子
parent.component.html
<ng-template #insertedElement1>
...
</ng-template>
<ng-template #insertedElement2>
...
</ng-template>
<ng-template #insertedElement3>
...
</ng-template>
<child-component>
<span #insertionPoint></span>
</child-component>
parent.component.ts
...
@ViewChild('insertionPoint', { static: false, read: ViewContainerRef })
insertionPoint: ViewContainerRef;
// static false because these elements have structural logic within them
@ViewChild('insertedElement1', { static: false })
insertedElement1: TemplateRef<any>;
@ViewChild('insertedElement2', { static: false })
insertedElement2: TemplateRef<any>;
@ViewChild('insertedElement3', { static: false })
insertedElement3: TemplateRef<any>;
// ngAfterViewInit to let the structural logic complete
ngAfterViewInit() {
// After timeout or I get ExpressionChangedAfterItHasBeenCheckedError
setTimeout(() => {
// This array is fixed for this example, but would be dynamic
[
"insertedElement3"
"insertedElement1"
"insertedElement2"
].forEach(element => {
this.insertionPoint.createEmbeddedView(this[element]);
});
});
}
...
解决方案
推荐阅读
- ios - 我可以在 Xcode 上为横向和纵向方向创建多个启动画面吗?
- c# - Canvas.Left 属性以哪个单位测量?
- asp.net - 如何在模态弹出窗口中清除 iframe 标签的多个 youtube url?
- scala - 动态合并 Akka 流
- google-apps-script - 使用应用程序脚本将电子表格表格嵌入谷歌文档
- node.js - 以通用方式解析简单值类型
- jquery - 无法从 AJAX 编辑现有记录时填充两列多选
- angular-cli - 使用 ng --version 命令时出现错误而不是 Angular 的版本
- apache - 无法从 aws 的公共 ip 访问 apache 默认页面
- typo3 - 从 TYPO3 7 更新到 TYPO3 9 LTS 后的 Extbase / 类型问题