I'm working on reactive forms in Angular 8. There is a requirement where I need to load the html from database and show it on page.
Based on the answers mentioned in the this link, I wrote the code. Angular HTML binding
in html page
<div class="col-md-10" [innerHTML]="genericHtml | keepHtml"></div>
created a new pipe as follows
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({ name: 'keepHtml', pure: false })
export class EscapeHtmlPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {
}
transform(content) {
return this.sanitizer.bypassSecurityTrustHtml(content);
}
}
setting the variable like this in component class
getGlobalMessage(): void {
this.setupService.getMessage(true).subscribe(
(setupModel: SetupModel) => this.onSetupRetrieved(setupModel),
(error: any) => this.errorMessage = <any>error
);
}
onSetupRetrieved(setupModel: SetupModel): void {
this.genericHtml = this.setupEdit.html_content; //--> setting the variable from db value here
console.log(this.genericHtml);
}
I also set the encapsulation in Componenet header on the class
@Component({
selector: 'home',
templateUrl: './home.component.html',
encapsulation: ViewEncapsulation.None
})
This works fine for the first time.
ISSUE It works fine the first time but when I navigate to other page and comes back to this page again. the html from the database loads in the variable but I can't see it on the page. I can see the html in the console.