HTML Decode

djordjeg
Giga Contributor

I am making Service Portal Widget where I retrieve a Knowledge Article Text which is already formatted and styled.

Text, when retrieved from GlideRecord looks like normal HTML, i.e : <p>Test Article</p><br/><div><b>Test Div</b></div> <p>List of current projects:</p><ul><li>List1</li><li>List2</li></ul><br/><p>test2</p>

When I try to assign this value to a div's innerHTML in widget's Body HTML template I get the exact same thing, HTML: <p>Test Article</p><br/><div><b>Test Div</b></div><ul><li>List1</li><li>List2</li></ul><br/><p>test2</p>. I want this to be parsed like any other html and show like:

Test Article

Test Div

List of Current projects:

  • List1
  • List2

test2

I tried HTML decoding using 2 divs and assigning the value to the innerText first and then copying to the innerHTML of the second, but I always get HTML. Is there something that maybe I can try in the server code before returning the text? I tried a.Text=JSUtil.unescapeText(z.getValue('text')), but still the same. How can I get the browser to parse this string as HTML and not as a text?

Any ideas?

Thanks,

Goran

1 ACCEPTED SOLUTION

nathanfirth
Tera Guru

Hi Goran,



You have to use the Angular.js $sce service to render HTML.



In your controller pass in "$sce" to the function:


function($sce) {


        var c = this;


        c.data.html = $sce.parseAsHtml(data.html)


}



And then in your HTML use:


<div ng-bind-html="c.data.html"></div>


View solution in original post

2 REPLIES 2

nathanfirth
Tera Guru

Hi Goran,



You have to use the Angular.js $sce service to render HTML.



In your controller pass in "$sce" to the function:


function($sce) {


        var c = this;


        c.data.html = $sce.parseAsHtml(data.html)


}



And then in your HTML use:


<div ng-bind-html="c.data.html"></div>


Thanks Nathan!