Angular 2获取应用程序的ViewContainerRef

Angular 2获取应用程序的ViewContainerRef

问题描述:

我想动态创建一个模态组件并将该组件附加到body或我的根元素。Angular 2获取应用程序的ViewContainerRef

我知道,当你需要动态添加组件时,你需要一个ViewContainerRef

例如:

createComponent(type) { 
    this.container.clear(); 
    const factory: ComponentFactory = this.resolver.resolveComponentFactory(AlertComponent); 
    this.componentRef: ComponentRef = this.container.createComponent(factory); 
    } 

如何追加成分的根组件?

您应该在您的rootcomponent html文件中使用选择器