Rajeev
Rajeev

Reputation: 46909

Angularjs render html in special characters

I was not very clear in my last question. However after seeing the response from the server asking the same again. The html is not getting rendered here how to fix this

AppControllers.controller('create', ['$scope','$sce', function ($scope,$sce){
    var myhtml  = "<div class="modal-body " id="mymodal">
<form name="form" novalidate="" class="add-user">
<fieldset>";
    $scope.myhtml= $sce.trustAsHtml(myhtml)
    console.log($scope.myhtml)  
    /* Output of console.log
 <div class="modal-body " id="mymodal">
   <form name="ticketform" novalidate=""    class="add-user-from "&gt
    */ 
}

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

Upvotes: 0

Views: 4818

Answers (1)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

try something like this,

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

add jquery also.

var myhtml  = "&lt;div class=&quot;modal-body &quot; id=&quot;mymodal&quot;&gt;
&lt;form name=&quot;&quot; novalidate=&quot;&quot; class=&quot;add-user &quot;&gt;
&lt;fieldset&gt;";

$scope.decodedText = $('<textarea />').html(myhtml).text();


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

DEMO

OR IF you prefer No Jquery use this,

var myhtml = "&lt;div class=&quot;modal-body &quot; id=&quot;mymodal&quot;&gt;&lt;form name=&quot;ticketform&quot; novalidate=&quot;&quot; class=&quot;add-user-from add_ticket&quot;&gt;&lt;fieldset&gt;";

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

$scope.decodedText = txt.value;

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

DEMO

Upvotes: 4

Related Questions