Is there a way to insert dynamically a component as a child (not a sibling) of a DOM tag in Angular 2?
There are plenty of examples around there to
My dirty way, just save the reference of dynamically created component (sibling) and move it using vanilla JS:
constructor(
private el: ElementRef,
private viewContainerRef: ViewContainerRef,
private componentFactoryResolver: ComponentFactoryResolver,
) {}
ngOnInit() {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
MyDynamicComponent,
);
const componentRef = this.viewContainerRef.createComponent(componentFactory);
this.el.nativeElement.appendChild(componentRef.location.nativeElement);
}