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 in a way it’s a platform-independent approach.