在Angular中unescape html实体的正确方法是什么?

时间:2017-11-19 14:35:05

标签: angular escaping

我从json文件中获取html实体,例如:@Entity public class User { @Version @Column(nullable = false) private Long version; } 我怎样才能在html组件中取消它?

我创建了自定义管道,但它仅适用于像$startTime = "16:15"; $endTime = "22:45"; $startTimeObject = new DateTime($startTime); $endTimeObject = new DateTime($endTime); $interval = new DateInterval('PT15M'); $times = []; $currentTime = $startTimeObject; while(true) { if ($currentTime->format("H:i") === $endTimeObject->format("H:i")) { break; } $times[] = $currentTime->format("H:i"); $currentTime->add($interval); } foreach ($times as $time) { echo "<option>$time</option>\n"; } 这样的实体:

&#13;
&#13;
&#8217;
&#13;
&#13;
&#13;

  • 我正在与Angular 5合作。

2 个答案:

答案 0 :(得分:1)

仍然相同:使用replace或encodeUri JavaScript并逃避你不想要的每个字符。另一种方法是创建一个像你正在做的管道基于正则表达式/替换/转义函数;)

escape(htmlInput){htmlInput.replace(“&amp;”,“and”)}

编辑:escape现在是encodeUri,如果您有匹配的模式,您还可以使用包来验证:)

答案 1 :(得分:0)

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

&#13;
&#13;
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;
  }

}
&#13;
&#13;
&#13;