我从 json 文件中获取 html 实体,例如:’
如何在 html 组件中取消转义它?
我创建了自定义管道,但它仅适用于以下实体&:
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);
}
}Run Code Online (Sandbox Code Playgroud)
解决方案,创建下一个自定义管道:
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;
}
}Run Code Online (Sandbox Code Playgroud)
还是一样的:使用JavaScript的replace或encodeUri并转义每个你不想要的字符。另一种方法是创建一个 Pipe,就像您基于正则表达式/替换/转义函数所做的那样;)
escape(htmlInput) {
htmlInput.replace("&", "and")
}
Run Code Online (Sandbox Code Playgroud)
escape现在是encodeUri,您还可以使用 contains 首先验证是否有匹配的模式:)
| 归档时间: |
|
| 查看次数: |
5806 次 |
| 最近记录: |