Angular HTML binding

后端 未结 21 2625
暖寄归人
暖寄归人 2020-11-21 04:00

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

21条回答
  •  执笔经年
    2020-11-21 04:58

    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:
    

提交回复
热议问题