[英]How do I add an attribute to a dynamically created element in Angular2+?
[英]Compile angular2+ component to dynamically created DOM element
我有一個第三方庫,可以動態創建DOM元素,我需要編譯一個組件並將其插入到創建的DOM元素中。
我正在使用組件工廠解析器,我知道我可以將其插入ViewContainerRef
,但是我不確定如何插入由外部angular lib創建的普通DOM元素。
onDynamicDomCreate: (e: GridChangeEvent) => {
let factory = this.componentFactoryResolver.resolveComponentFactory(UserDetailsComponent);
e.sender.element.createComponent(factory); // <--- how to properly create component here?
}
創建這樣的組件時,您需要傳遞DOM元素:
let factory = this.componentFactoryResolver.resolveComponentFactory(UserDetailsComponent);
const compRef = factory.create(injector, [], e.sender.element);
並在ApplicationRef中注冊創建的視圖,否則您將沒有組件更改檢測:appRef.attachView(compRef.hostView);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.