我正在尝试创建具有显示任意模态的能力的模态服务.目前,要创建动态组件,我在视图中存储占位符:
<div #container></div>
...
@ViewChild("dialogContainer", {read: ViewContainerRef})
dialogContainer:ViewContainerRef;
Run Code Online (Sandbox Code Playgroud)
而不是创建组件:
let factory = this.componentResolver.resolveComponentFactory(Dialog);
this.componentReference = this.dialogContainer.createComponent(factory);
Run Code Online (Sandbox Code Playgroud)
有没有办法将所有这些逻辑从组件转移到服务,能够在正文或任何其他DOM元素中创建/删除视图容器?
我正在使用可自定义的单元格配置创建网格组件.目前,为了能够在单元配置中设置自定义单元组件,我在视图中存储了一大堆模板,如下所示:
<template let-row="row" #nameCell>
<name-cell [row]="row"></name-cell>
</template>
Run Code Online (Sandbox Code Playgroud)
在视图中获取模板并存储在网格配置中:
@ViewChild('nameCell') nameCell: TemplateRef<any>;
...
column.customCell = nameCell
Run Code Online (Sandbox Code Playgroud)
而不是在网格单元格中使用此模板:
<template
*ngIf="column.customCell"
[ngTemplateOutlet]="column.customCell"
[ngOutletContext]="{ row: row}">
</template>
Run Code Online (Sandbox Code Playgroud)
这有效,但看起来凌乱和肮脏.有没有办法从外部文件动态加载TemplateRef?