前端:Angular框架与Ionic框架集成Html翻译pipe(管道)

news/2024/7/4 23:26:42

在前端开发的时候经常会遇到页面显示的内容有HTML标记。默认是没有经过翻译的。集成翻译管道就可以正常显示内容。由于Angular与ionic集成的方法有点不,所以分开写。

一、Angular框架集成pipe

1、新建一个管道

新建命令:ng g pipe pipe/tohtml
tohtml.pipe.ts文件代码:
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({
  name: 'tohtml' //使用名称
})
export class TohtmlPipe implements PipeTransform {
//构造方法中加入DomSanitizer
  constructor(private _sanitizer: DomSanitizer){}
  transform(html: any, args?: any): any {
  //获取到HTML内容,对html进行转换
    return this._sanitizer.bypassSecurityTrustHtml(html);
  }
}

2、在使用的地方进行引用

 <div [innerHTML]="n.content | tohtml">

二、Ionic框架集成pipe

1、新建一个管道

新建命令:ionic g pipe pipes/html
html.pipe.ts文件代码
import { Pipe, PipeTransform } from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';

@Pipe({
    name: 'html'
})
export class HtmlPipe implements PipeTransform {

  //将DomSanitizer添加到构造方法中
    constructor(private _sanitizer: DomSanitizer){}
    transform(value: any, ...args: any[]): any {
      //这里返回的一个html内容,将value进行转换
        return this._sanitizer.bypassSecurityTrustHtml(value);
    }
}

2、集成module,进行模块化

新建命令:ionic g module pipes/pipes
pipes.module.ts文件代码:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {HtmlPipe} from "../html.pipe";


@NgModule({
  declarations: [HtmlPipe],
  imports: [
    CommonModule
  ],
    exports:[HtmlPipe]
})
export class PipesModule { }

3、去掉app.module.ts文件的declarations版块的HtmlPipe声明

image

4、在使用的模块的module里面加上PipesModule

例如在newsdetail.module.ts里面的imports版块加入PipesModule

image

5、HTML部分

<div [innerHTML]="news.content | html"></div>

5、对比效果

image


http://www.niftyadmin.cn/n/3649651.html

相关文章

bem css_如何使用ITCSS和BEM解决大规模CSS瓶颈

bem css介绍 (Introduction) On frontend codebase projects, requirements and sometimes scopes can change frequently. When requirements change and you adjust the stylesheet or extend the styles of a selector in a particular page, such changes often affects ot…

Java基础---多态、抽象类、接口

2015-4-4 一、多态 1、定义&#xff1a;某一个事物&#xff0c;在不同时刻表现出来的不同状态。 2、多态的前提与体现&#xff1a; &#xff08;1&#xff09;要有继承关系&#xff1b; &#xff08;2&#xff09;要进行方法的重写&#xff1b; &#xff08;3&#xff09;要父…

移动开发:ionic框架的Android开发环境搭建

一、解决Eclipse与Android Studio的SDK冲突 Eclipse与Android Studio不能共用一套Android SDK&#xff0c;Android Studio会改变SDK的结构&#xff0c;eclipse不能正常使用Android SDK。但是我这个学期的两个课程都与Android开发有关。而且两个课程使用的开发工具都不一样。一边…

[流媒体]实例解析MMS流媒体协议,下载LiveMediaVideo[2][修正版本]

编写者日期关键词郑昀ultrapower2005-10-17mms streaming protocol ethereal 协议分析 流媒体为了改造mimms&#xff0c;我分析了SDP和流媒体服务器的来往包&#xff0c;看看我和他的实现到底存在哪些差异。如果你也开发流媒体下载应用&#xff0c;希望这个分析对你理解 “Micr…

react集成钉钉api_如何将Google Maps API集成到React应用程序中

react集成钉钉api介绍 (Introduction) This tutorial aims at integrating the google maps API to your React components and enabling you to display maps on your website. 本教程旨在将google maps API集成到您的React组件中&#xff0c;并使您能够在网站上显示地图。 先…

Android项目实战系列—基于博学谷(四)我的模块(上)

由于这个模块内容较多,篇幅较长&#xff0c;请耐心阅读。 “我”的模块分为四个部分 我的界面 设置界面 修改密码界面 设置密保和找回密码 一、“我”的界面 1、底部导航栏 &#xff08;1&#xff09;、导入界面图片 将底部导航栏所需图片main_course_icon.png、main_course…

[流媒体]实例解析MMS流媒体协议,下载LiveMediaVideo[1]

[流媒体]按照MMS协议和MS Media Server交互下载实时交通录像的包分析---实例解析MMS流媒体协议&#xff0c;下载LiveMediaVideo编写者日期关键词郑昀ultrapower2005-10-17mms streaming protocol ethereal 协议分析 流媒体通过mms://220.194.63.17/cebeijing8&#xff0c;我们可…