TypeScript - Append HTML to container element in Angular 2

后端 未结 5 698
鱼传尺愫
鱼传尺愫 2020-12-04 14:39

What I want to do, is simply append some html on an element. I checked some links, and i find different confusing/non-working/non-recomended/etc solutions.

Using jav

5条回答
  •  有刺的猬
    2020-12-04 14:59

    With the new angular class Renderer2

    constructor(private renderer:Renderer2) {}
    
      @ViewChild('one', { static: false }) d1: ElementRef;
    
      ngAfterViewInit() {
        const d2 = this.renderer.createElement('div');
        const text = this.renderer.createText('two');
        this.renderer.appendChild(d2, text);
        this.renderer.appendChild(this.d1.nativeElement, d2);
      }
    

提交回复
热议问题