myComp
是具有可投影内容的组件:
@Component({
selector: 'my-comp',
template: `
<div>
<h1>Component with ng-content</h1>
<ng-content></ng-content>
</div>
`
})
export class myComp {}
constructor(resolver: ComponentFactoryResolver, container: ViewContainerRef) {
const factory = resolver.resolveComponentFactory(myComp);
const component = container.createComponent(container);
// append projectableNodes to component in a later procedure.
}
初始化后,是否可以将projectableNodes
附加到此组件?
我知道如果在同一个参数中创建它们,则有一种直接的方法:
const ngContentChildren = [childComp.location.nativeElement, childComp2.location.nativeElement];
const component = container.createComponent(container, 0, undefined, [ngContentChildren]);
但是我希望有一些功能类似于:
const component = container.createComponent(container);
component.project([children]);
因此可以稍后再添加孩子