How to append dynamic DOM elements from a directive in Angular 2?
Use Renderer provided by Angular to manipulate the DOM: import { DOCUMENT } from ‘@angular/common’; export class MyDirective implements OnInit { constructor( private elementRef: ElementRef, private renderer: Renderer2, @Inject(DOCUMENT) private document: Document) { } ngOnInit() { const child = this.document.createElement(‘div’); this.renderer.appendChild(this.elementRef.nativeElement, child); } } This doesn’t depend on the native DOM API like appendChild(), so … Read more