w3schools tag style strong color change javascript html angularjs html-entities utf8-decode

javascript - tag - Decodificar entidad HTML en Angular JS



title html (3)

Puede usar la directiva ng-bind-html para mostrarla como un contenido html con todas las entidades html descodificadas. Solo asegúrese de incluir la dependencia ngSanitize en su aplicación.

DEMO

JAVASCRIPT

angular.module(''app'', [''ngSanitize'']) .controller(''Ctrl'', function($scope) { $scope.html = ''"12.10 On-Going Submission of ""Made Up"" Samples."''; });

HTML

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

¿Cómo decodifico la entidad HTML en el texto usando JS angular.

Tengo la cuerda

"&quot;12.10 On-Going Submission of &quot;&quot;Made Up&quot;&quot; Samples.&quot;"

Necesito una forma de decodificar esto usando Angular JS. Encontré una forma de hacerlo usando javascript here pero estoy seguro de que eso no funcionará para Angular. Necesito recuperar la cadena original en la interfaz de usuario que se vería como

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


Si no quieres usar ngSanitize, puedes hacerlo de esta manera:

en su controlador:

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

Y en la plantilla:

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

Solo asegúrate de inyectar $ sce en tu controlador


Tengo un problema similar, pero no necesito usar el valor del resultado en la interfaz de usuario. Este problema se resolvió mediante el código del módulo angular ngSanitize:

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); document.getElementById("encoded").innerText=encoded; document.getElementById("decoded").innerText=decoded;

#encoded { color: green; } #decoded { color: red; }

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