什么正确的方法来在Angular中转义html实体? [英] What the right way unescape html entities in Angular?

查看:70
本文介绍了什么正确的方法来在Angular中转义html实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从json文件中获取html实体,例如:’ 如何在html组件中取消转义?

I get html entities from json file, like: ’ How can I unescape it in html component?

我创建了自定义管道,但它仅适用于&这样的实体:

I created custom pipe, but it works only for entities like &:

import { Pipe, PipeTransform } from '@angular/core';
import {unescape} from 'lodash';

@Pipe({
  name: 'unescape'
})
export class UnescapePipe implements PipeTransform {

  transform(value: any, args?: any): any {
    return unescape(value);
  }

}

  • 我正在使用Angular 5.

推荐答案

解决方案,创建下一个自定义管道:

The solution, create next custom pipe:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'unescape'
})
export class UnescapePipe implements PipeTransform {

  transform(value: any, args?: any): any {
    const doc = new DOMParser().parseFromString(value, 'text/html');
    return doc.documentElement.textContent;
  }

}

这篇关于什么正确的方法来在Angular中转义html实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆