Angular HTML binding

后端 未结 21 3275
暖寄归人
暖寄归人 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:40

    You can apply multiple pipe for style, link and HTML as following in .html

    and in .ts pipe for 'URL' sanitizer

    import { Component, Pipe, PipeTransform } from '@angular/core';
    import { DomSanitizer } from '@angular/platform-browser';
    
    @Pipe({ name: 'safeUrl' })
    export class SafeUrlPipe implements PipeTransform {
        constructor(private sanitizer: DomSanitizer) {}
        transform(url) {
            return this.sanitizer.bypassSecurityTrustResourceUrl(url);
        }
    }
    

    pipe for 'HTML' sanitizer

    import { Component, 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) {
            return this.sanitized.bypassSecurityTrustHtml(value);
        }
    }
    

    this will apply both without disturbing any style and link click event

提交回复
热议问题