欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

angular富文本解析,内置样式不生效问题

程序员文章站 2022-06-07 14:07:57
...

1.新建管道 pipe/htmlpipi.pipe.ts文件

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from "@angular/platform-browser";
@Pipe({
  name: 'htmlpipe'
})
export class HtmlpipePipe implements PipeTransform {

  constructor(private sanitizer: DomSanitizer) {
  }
  transform(style) {
    return this.sanitizer.bypassSecurityTrustHtml(style);
  }

}

2.在公共文件modules/share/share.module.ts文件引入

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DebounceClickDirective } from '../../directives/debounce-click.directive';
import { HtmlpipePipe } from '../../pipe/htmlpipe.pipe';

@NgModule({
    declarations: [
        DebounceClickDirective,
        HtmlpipePipe
    ],
    imports: [
        CommonModule
    ],
    exports: [
        DebounceClickDirective,
        HtmlpipePipe
    ]
})
export class ShareModule { }

3.在使用页面引入

// 防抖指令
import { ShareModule } from '../../modules/share/share.module';
imports: [
    ShareModule
  ],

4.在页面使用

<ion-content>
  <h5 [innerHTML]="newsdetail.title"></h5>
  <div class="content" [innerHTML]="newsdetail.content | htmlpipe"></div>
</ion-content>

 

相关标签: angualr