you can try by using ElementRef
, if you want to append element not want to replace full html ,
html
<div #Container>
add here
</div>
ts file
export class AppComponent implements AfterViewInit {
@ViewChild('Container') container: ElementRef ;
content = [
{judul: "Judul 1", isi:"<div id='title_1'>Isinya</div>"},
{judul: "Judul 2", isi:"<div id='title_2'>pranay</div>"},
];
ngAfterViewInit() {
this.content.forEach( (item,index) =>{
this.container.nativeElement.insertAdjacentHTML('beforeend', `<div id="title${index}">${item.judul}</div>`);
this.container.nativeElement.insertAdjacentHTML('beforeend', `${item.isi}`);
});
}
however elementref is not recommended way.
output of above code

you can see it adding content after , add here one by one
Find : Working Demo
Instead of using function in your component (its not working as your html-template trying to pass html to your typscript file) , you should create pipe as below
html.pipe.ts
import { Component, Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: 'html'
})
export class HtmlPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {}
transform(value) {
return this.sanitized.bypassSecurityTrustHtml(value);
}
}
and use it like
html
<div *ngFor="let item of content">
<h3 [innerHTML]="item.judul | html ">add</h3>
<div [innerHTML]="item.isi | html "><p>new word</p></div>
</div>
Find : Working Demo