TypeScript - Append HTML to container element in Angular 2

后端 未结 5 713
鱼传尺愫
鱼传尺愫 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

    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.

      {{string}}

    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.

提交回复
热议问题