AngularJs: How to decode HTML entities in HTML?

22,284

Solution 1

I think you need to perform one more "decoding" step before you pass it to $sce. For example like this:

app.filter('trusted', ['$sce', function($sce) {
    var div = document.createElement('div');
    return function(text) {
        div.innerHTML = text;
        return $sce.trustAsHtml(div.textContent);
    };
}]);

Demo: http://plnkr.co/edit/LrT4tgYtTu4CPrOAidra?p=preview

Solution 2

Add the angular.sanitize.js

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-sanitize.js"></script>

add the dependency as,

var app = angular.module('plunker', ['ngSanitize']);

NOW Decode the html string and pass it to ng-bind-html.

$http.get('http://API/page_content').then(function(resp) 
{
    var html = resp.data[0].content; 

    var txt = document.createElement("textarea");
    txt.innerHTML = html;


    $scope.content_test = txt.value;

    //if you use jquery then use below
    //$scope.content_test = $('<textarea />').html(html).text();        
}

<div ng-bind-html="content_test"></div>

I think you can avoid the filter see the below example.

example

Share:
22,284
FrancescoMussi
Author by

FrancescoMussi

Full-stack developer based in Riga, Latvia. Hope Socrates is proud of my Socratic badge on StackOverflow.

Updated on July 09, 2022

Comments

  • FrancescoMussi
    FrancescoMussi almost 2 years

    THE SITUATION:

    I am bulding a webpage which content is taken calling an API that returns the data in json format.

    The problem is that the html tags are given as HTML entities, that has to be decoded.

    EXAMPLE:

    This is example of the json i am dealing with:

    &#60;p align=&#34;justify&#34;&#62;&#60;strong&#62;15&#60;sup&#62;th&#60;/sup&#62; HERE THERE IS A BOLD TEXT &#60;/strong&#62; HERE SOME NORMAL TEXT...
    

    ATTEMPT:

    I have spend time research it and it seems harder than i thought. Looking in google and similar SO question, a possible solution is to use the ng-bing-html

    Api call:

    $http.get('http://API/page_content').then(function(resp) 
    {
        $scope.content_test = resp.data[0].content; 
    }
    

    Filter:

    .filter('trusted', ['$sce', function($sce){
        return function(text) {
            return $sce.trustAsHtml(text);
        };
    }])
    

    Ng-bind-html in the angular view:

    <div ng-bind-html=" content_test  | trusted"></div>
    

    OUTPUT:

    This is the output in the view (exactly as you see it):

    <p align="justify"><strong>15<sup>th<\/sup> HERE THERE IS A BOLD TEXT<\/strong> HERE SOME NORMAL TEXT...
    

    but what i need to see is the text properly formatted:

    HERE THERE IS A BOLD TEXT HERE SOME NORMAL TEXT...

    THE QUESTION:

    How can i decode HTML entities in a proper formatted HTML in AngularJs?

  • FrancescoMussi
    FrancescoMussi almost 9 years
    Thank you very much! Is working properly! I accepted the other only because it can be more re-usable, and take less code. But your answer is correct too. I can give a plus and specify it in an edit to my question.
  • Michal Stefanow
    Michal Stefanow about 4 years
    trust - don't trust, verify. It will not work for me.