TypeScript - Append HTML to container element in Angular 2

后端 未结 5 699
鱼传尺愫
鱼传尺愫 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);
      }
    
    0 讨论(0)
  • 2020-12-04 14:59

    There is a better solution to this answer that is more Angular based.

    1. Save your string in a variable in the .ts file

      MyStrings = ["one","two","three"]

    2. In the html file use *ngFor.

      <div class="one" *ngFor="let string of MyStrings; let i = index"> <div class="two">{{string}}</div> </div>

    3. if you want to dynamically insert the div element, just push more strings into the MyStrings array

      myFunction(nextString){ this.MyString.push(nextString) }

    this way every time you click the button containing the myFunction(nextString) you effectively add another class="two" div which acts the same way as inserting it into the DOM with pure javascript.

    0 讨论(0)
  • 2020-12-04 15:01

    1.

    <div class="one" [innerHtml]="htmlToAdd"></div>
    
    this.htmlToAdd = '<div class="two">two</div>';
    

    See also In RC.1 some styles can't be added using binding syntax

    1. Alternatively
    <div class="one" #one></div>
    
    @ViewChild('one') d1:ElementRef;
    
    ngAfterViewInit() {
      d1.nativeElement.insertAdjacentHTML('beforeend', '<div class="two">two</div>');
    }
    

    or to prevent direct DOM access:

    constructor(private renderer:Renderer) {}
    
    @ViewChild('one') d1:ElementRef;
    
    ngAfterViewInit() {
      this.renderer.invokeElementMethod(this.d1.nativeElement', 'insertAdjacentHTML' ['beforeend', '<div class="two">two</div>']);
    }
    
      3.
    constructor(private elementRef:ElementRef) {}
    
    ngAfterViewInit() {
      var d1 = this.elementRef.nativeElement.querySelector('.one');
      d1.insertAdjacentHTML('beforeend', '<div class="two">two</div>');
    }
    
    0 讨论(0)
  • 2020-12-04 15:01

    When working with Angular the recent update to Angular 8 introduced that a static property inside @ViewChild() is required as stated here and here. Then your code would require this small change:

    @ViewChild('one') d1:ElementRef;
    

    into

    // query results available in ngOnInit
    @ViewChild('one', {static: true}) foo: ElementRef;
    

    OR

    // query results available in ngAfterViewInit
    @ViewChild('one', {static: false}) foo: ElementRef;
    
    0 讨论(0)
  • 2020-12-04 15:05

    You could do something like this:

    htmlComponent.ts

    htmlVariable: string = "<b>Some html.</b>"; //this is html in TypeScript code that you need to display

    htmlComponent.html

    <div [innerHtml]="htmlVariable"></div> //this is how you display html code from TypeScript in your html

    0 讨论(0)
提交回复
热议问题