You can link the html using the [innerHtml] tag. This way you do not need a viewchild .:
<div [innerHtml]="htmlText"></div>
component:
export class BuilderComponent { htmlText: string; ngAfterViewInit() { } addRow() { this.htmlText = this.htmlText + `<div #row class="row"> <div class="s12 teal lighten-2"> <p class="flow-text">div inside parent - html builder</p> </div> </div>`; } }
Another solution that I would use correctly is to create an array of strings and use * ngFor and a pattern to scroll through it. By doing so, you can avoid HTML in typescript, and you don't need to edit html both places with every update.
For instance:
Template:
<div *ngFor="let row of rows" class="row"> <div class="s12 teal lighten-2"> <p class="flow-text">{{ row }}</p> </div> </div>
component:
export class BuilderComponent { rows: Array<string> = new Array(); ngAfterViewInit() { } addRow() { this.rows.push("Test 123 text"); } }
source share