【发布时间】:2018-10-30 12:57:45
【问题描述】:
这里是函数,我用来动态添加带有额外 HTML 元素的组件。我正在尝试在哪个元素中添加 Angular 自定义指令和属性。它不工作。 :(
addComponent(component:any){
let componentRef = this.componentFactoryResolver
.resolveComponentFactory(component)
.create(this.injector);
this.appRef.attachView(componentRef.hostView);
const domElem = (componentRef.hostView as EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
var newcontent = document.createElement('div');
newcontent.innerHTML = `<h1 myDirective [myAttr]="myVar" myAttr2="myVar2">${this.demoText}</h1>`;
domElem.appendChild(newcontent);
document.getElementById("testid").appendChild(domElem);
}
这里 myDirective 是一个自定义 Angular 指令。 myAttr, myAttr2 是自定义 Angular 指令的属性。 myVar、myVar2 可以是静态文本,也可以是同一个组件的变量。
【问题讨论】:
标签: javascript html angular typescript