【问题标题】:Style not working for innerHTML in ionic "Angular"样式不适用于离子“Angular”中的innerHTML
【发布时间】:2021-01-03 10:38:45
【问题描述】:

当我尝试将 html 作为 innerHtml 传递给我的视图样式组件时不起作用

<div [innerHTML]="Html Code"></div>

【问题讨论】:

    标签: innerhtml


    【解决方案1】:

    为了解决这些问题,使用 DomSanitizer 能够渲染 CSS 绕过。

    1. 生成管道:

      ionic g pipe safeHtml

    2. 通过以下方式修改文件管道内的所有代码:

      从 '@angular/core' 导入 { Pipe, PipeTransform }; 从“@angular/platform-b​​rowser”导入 {DomSanitizer};

      @管道({ 名称:'safeHtml', }) 导出类 SafeHtmlPipe 实现 PipeTransform {

      构造函数(私人消毒剂:DomSanitizer){} 转换(html){ 返回 this.sanitizer.bypassSecurityTrustHtml(html); } }

    3. 将 crated 的管道添加到 app/app.module.ts :

      @NgModule({ 声明:[ 我的应用程序, ... SafeHtmlPipe, ],

    4. 在您使用 innerHTML 的 HTML 文件中的最后一个点添加管道:

    【讨论】: