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.

klode
  • 10,821
  • 5
  • 34
  • 49
  • 1
    Possible duplicate of [HTML Entity Decode](http://stackoverflow.com/questions/5796718/html-entity-decode) – Noy Feb 16 '16 at 17:44
  • Possible duplicate of [Decode HTML entity in Angular JS](http://stackoverflow.com/questions/26064309/decode-html-entity-in-angular-js) – Damien Fayol Feb 16 '16 at 17:46

2 Answers2

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;
}
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

You should check out this item: ngModel Formatters and Parsers

Just use $formatters to change the model value.

Community
  • 1
  • 1
Brent Theunckens
  • 105
  • 1
  • 13