I have a <div>
with a directive [appComponent]
so I can create a component and append to it.
<div [appComponent]="component">
My directive is as follows:
@Directive({
selector: '[appComponent]'
})
export class ComponentDirective implements OnChanges {
@Input('appComponent') comp: any;
private container;
constructor(private elementRef: ElementRef, private renderer: Renderer2) {
this.container = this.elementRef.nativeElement;
}
ngOnChanges(changes: SimpleChanges): void {
const newComponent = this.renderer.createElement('new-component');
this.renderer.appendChild(this.container, newComponent);
}
}
And my new-component
:
@Component({
selector: 'new-component',
templateUrl: './new-component.html',
styleUrls: ['./new-component.scss']
})
export class NewComponent {
}
This component has contents inside. The element is created and appended to div, however, it's empty:
<div>
<new-component _ngcontent-c3=""></new-component>
</div>
How to instantiate a component and append it to div? I'm using angular 7.