简体   繁体   English

Angular:控制器中的HTML解码和编码

[英]Angular: HTML decoding and encoding in controller

In our Angular app we are using an pi that returns strings containing HTML encoded characters, for example it would return 'let's' where ' 在我们的Angular应用中,我们使用的pi返回包含HTML编码字符的字符串,例如,它将返回'let's' ,其中' is the encoded "single quote character" 是编码的“单引号字符”

I would like to create a function htmlDecode(myString) to return the decoded string in the javascript code, for example: 我想创建一个函数htmlDecode(myString)以在javascript代码中返回解码后的字符串,例如:

var myString = 'let's';
var decodedString = htmlDecode(myString);
console.log(decodedString);               // CONSOLE OUTPUT: "let's"

I looked at the $sce service but could not come up with a solution yet. 我查看了$sce服务,但还没有找到解决方案。

One way is to set the innerHTML and read the text 一种方法是设置innerHTML并读取文本

function htmlDecode (str) {
    var div = document.createElement("div");
    div.innerHTML = str;
    return div.textContent || div.innerText;
}

You should check out this item: ngModel Formatters and Parsers 您应该检查以下项目: ngModel Formatters和Parsers

Just use $formatters to change the model value. 只需使用$ formatters更改模型值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM