Decode HTML entity in Angular JS

How do i decode HTML entity in text using angular JS.

I have the string

""12.10 On-Going Submission of ""Made Up"" Samples.""

I need a way to decode this using Angular JS. I found a way to do that using javascript here but I am sure thats wont work for Angular. Need to get back the original string on the UI which would look like

""12.10 On-Going Submission of ""Made Up"" Samples.""



You can use the ng-bind-html directive to display it as an html content with all the html entities decoded. Just make sure to include the ngSanitize dependency in your application.



angular.module('app', ['ngSanitize'])

  .controller('Ctrl', function($scope) {
    $scope.html = '"12.10 On-Going Submission of ""Made Up"" Samples."';


  <body ng-controller="Ctrl">
    <div ng-bind-html="html"></div>

If you don't want to use ngSanitize, you can do it this way:

in your controller:

$scope.html = '&quot;12.10 On-Going Submission of &quot;&quot;Made Up&quot;&quot; Samples.&quot;'
$scope.renderHTML = function(html_code)
            var decoded = angular.element('<textarea />').html(html_code).text();
            return $sce.trustAsHtml(decoded);

And in the template:

<div ng-bind-html="renderHTML(html)"></div>

Just make sure you inject $sce in your controller


I have similar issue, but don't need to use result value on UI. This issue was resolved by code from angular ngSanitize module:

var hiddenPre=document.createElement("pre");
* decodes all entities into regular string
* @param value
* @returns {string} A string with decoded entities.
function decodeEntities(value) {
  if (!value) { return ''; }
  hiddenPre.innerHTML = value.replace(/</g,"&lt;");
  // innerText depends on styling as it doesn't display hidden elements.
  // Therefore, it's better to use textContent not to cause unnecessary reflows.
  return hiddenPre.textContent;

var encoded = '&lt;p&gt;name&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:xx-small;"&gt;ajde&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;da&lt;/em&gt;&lt;/p&gt;';
var decoded = decodeEntities(encoded);

#encoded {
  color: green;

#decoded {
  color: red;
Encoded: <br/>
<div id="encoded">


Decoded: <br/>
<div id="decoded">


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.