Angula2中innerHtml的问题

innerHtml报不安全问题

Angular2引入外部html时候,会报不安全提示,此时如果你信任html来源,则添加如下转换:

  1. 导入包

    import {DomSanitizer} from "@angular/platform-browser";
    
  2. constructor() 中添加

    public sanitizer: DomSanitizer
    
  3. 转换数据

    innerHtml = this.sanitizer.bypassSecurityTrustHtml('html信息');