Hacker
Hacker

Reputation: 7906

Display escaped html content properly when rendering in angularjs

I have text like below which comes from JSON

<div><div>All are lucky and contect is like

I want this to be displayed properly when rendered. How do i do this.

<p>{{bodydata}}</p>

Upvotes: 0

Views: 78

Answers (1)

Arun AK
Arun AK

Reputation: 4380

you can use $sce (Strict Contextual Escaping) for displaying HTML encoded characters

function myCtrl($scope,$sce){
    $scope.html = $sce.trustAsHtml('&lt;div&gt;&lt;div&gt;All are lucky and contect is like');
}
<!DOCTYPE html>
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    </head>
  <body>
    
<div ng-controller="myCtrl">
    <span ng-bind-html="html"></span>
</div>
  </body>
  </html>

Hope it works for you :)

Upvotes: 1

Related Questions