klode
klode

Reputation: 11051

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 ' 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.

Upvotes: 0

Views: 5302

Answers (2)

Brent Theunckens
Brent Theunckens

Reputation: 105

You should check out this item: ngModel Formatters and Parsers

Just use $formatters to change the model value.

Upvotes: 0

epascarello
epascarello

Reputation: 207501

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;
}

Upvotes: 2

Related Questions