当我们使用this.sanitizer.bypassSecurityTrustHtml(value)时如何停止重新渲染html?

时间:2020-02-13 09:03:56

标签: angular typescript

在以下情况下使用(this.sanitizer.bypassSecurityTrustHtml(value))时如何停止重新渲染html 角度5 +?

1 个答案:

答案 0 :(得分:2)

您可以创建管道以完成工作。从理论上讲,管道将缓存结果并返回与您传递相同参数相同的值:

在safe-html.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Pipe({ name: 'safeHtml' })
export class SafeHtmlPipe implements PipeTransform {

    constructor(private sanitizer: DomSanitizer) {}

    public transform(value: string): SafeHtml {
        return this.sanitizer.bypassSecurityTrustHtml(value);
    }
}

在您的实现中:

<img [src]="image | safeHtml" />