1

Is there any way to add dynamic element in to specific location without #localvariable (<input type='button' #localvariable (click)='doSomething()'>).

For example, given:

<td></td>

After compiling:

<td><input type='button' (click)='doSomething()'></td>
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376

1 Answers1

0

ComponentFactory.create allows to pass a selector or node as target parameter:

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

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

See also https://angular.io/api/core/ComponentFactory#create

Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506
  • using 'ComponentFactory.create' how can we pass data into component. and my second question Is it possible to pass element as a string instead of component. – Rajitha Rumesh Leelananda Oct 29 '17 at 08:36
  • `compRef.instance.someField = 'foo';` https://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468 also shows how to do it. – Günter Zöchbauer Oct 29 '17 at 08:56