I am writing an Angular application and I have an HTML response I want to display.
How do I do that? If I simply use the binding syntax {{myVal}}
it en
We can always pass html content to innerHTML
property to render html dynamic content but that dynamic html content can be infected or malicious also. So before passing dynamic content to innerHTML
we should always make sure the content is sanitized (using DOMSanitizer
) so that we can escaped all malicious content.
Try below pipe:
import { Pipe, PipeTransform } from "@angular/core";
import { DomSanitizer } from "@angular/platform-browser";
@Pipe({name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {
}
transform(value: string) {
return this.sanitized.bypassSecurityTrustHtml(value);
}
}
Usage: