0

In our Angular app we are using an pi that returns strings containing HTML encoded characters, for example it would return 'let's' where ' 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:

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.

2

2 Answers 2

2

One way is to set the innerHTML and read the text

function htmlDecode (str) {
    var div = document.createElement("div");
    div.innerHTML = str;
    return div.textContent || div.innerText;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should check out this item: ngModel Formatters and Parsers

Just use $formatters to change the model value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.