gpt4 book ai didi

javascript - Angular 6 使用管道将字符串转换为 Html

转载 作者:行者123 更新时间:2023-12-01 17:16:47 25 4
gpt4 key购买 nike

我的输入字符串是

  html:string="<a role="button" class=" fas fa-trash-alt fa-1x" title="Remove Link" href="#"></a>"

我希望将其转换为原始 html

管道:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser'
@Pipe({
name: 'htmldecoder'
})
export class HtmldecoderPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) { }
transform(value) {
return this.sanitized.bypassSecurityTrustHtml(value);
}

}

HTML 代码:

<div>
<div [innerHtml]="html | htmldecoder">
</div>
</div>

进入UI的输出是

<a role="button" class="fas fa-trash-alt fa-1x" title="Remove Link" href="#"></a>

预期的输出应该是按钮

最佳答案

在您的代码中进行以下更改,它会起作用,代码非常简单,易于 self 解释

应用程序组件.ts

  html = `html:string="&lt;a role="button" class=" fas fa-trash-alt fa-1x" title="Remove Link" href="#"&gt;aditya&lt;/a&gt;"`;

管道

  transform(value) {
value = value.substring(13, value.length-1);
var doc = new DOMParser().parseFromString(value, "text/html");
const value123 = doc.documentElement.textContent;
return this.sanitized.bypassSecurityTrustHtml(value123);
}

app.component.html

<div [innerHTML]="html | htmldecoder">

关于javascript - Angular 6 使用管道将字符串转换为 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62170801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com