将元素编译到特定位置,无需任何 #localvariable

Compile element to specific location without any #localvariable

有没有办法在没有 #localvariable (<input type='button' #localvariable (click)='doSomething()'>) 的情况下将动态元素添加到特定位置。

例如,给定:

<td></td>

编译后:

<td><input type='button' (click)='doSomething()'></td>

ComponentFactory.create 允许将选择器或节点作为目标参数传递:

constructor(private resolver: ComponentFactoryResolver, private injector: Injector) {}

ngAfterViewInit() {
  let factory = this.resolver.resolveComponentFactory(MyDynamicComponent);
  let compRef = factory.create(injector, [], '#idOfTarget'); 
}

另见 https://angular.io/api/core/ComponentFactory#create