所有特殊字符代码都不会使用Domsanitizer转换为相应的符号

问题描述 投票:1回答:1

我在字符串Ex:“Test'Name”中有Apostrophe(')代码,使用Domsanitizer我正在清理它以将'转换为Apostrophe符号。但是,如果我有双引号代码(“),它不会转换为相应的符号。不仅为此,(&)也没有从代码(&)转换为符号。下面是我到目前为止编写的代码。

我是否正确地进行转换,从代码到符号?

let txt = 'Test"Name';
let txt2 = 'Test'Name';
txt = this.sanitizer.sanitize( SecurityContext.HTML, txt );
txt2 = this.sanitizer.sanitize( SecurityContext.HTML, txt2 );

const ul = this.renderer.createElement('ul');
const li = this.renderer.createElement('li');
const li2 = this.renderer.createElement('li');
const text = this.renderer.createText(txt);
const text2 = this.renderer.createText(txt2);

this.renderer.appendChild(li, text);
this.renderer.appendChild(li2, text2);
this.renderer.appendChild(ul, li);
this.renderer.appendChild(ul, li2);
this.renderer.appendChild(this.el.nativeElement, ul);

请在这里找到Stackblitz

问题1:某些代码未转换为相应的符号

问题2:如果我有一些像é这样的法语字符,那么它将被转换为代码,而不是显示相同的内容(也更新了stackblitz)

javascript angular typescript angular-directive
1个回答
1
投票

在您的情况下,清理程序将值转换为在浏览器DOM中安全使用。

因此,您需要将转换后的值分配给innerHTML(而不是文本节点)。

const ul = this.renderer.createElement('ul');
const li = this.renderer.createElement('li');
const li2 = this.renderer.createElement('li');
li.innerHTML = txt;   // <- inner html
li2.innerHTML = txt2; // <- inner html

this.renderer.appendChild(ul, li);
this.renderer.appendChild(ul, li2);
this.renderer.appendChild(this.el.nativeElement, ul);
© www.soinside.com 2019 - 2024. All rights reserved.