Skip to content Skip to sidebar Skip to footer

AngularJS Parse String To Html

I am trying to parse string to html using $sce, but it doesn't work. My function on angular controller: function renderHtml(string) { return $sce.trustAsHtml(string); } html:

Solution 1:

I have created a JSBin for your scenario and it works fine. The changes I have done are exposing the controller method, changing use of reserved word string to str and adding $sce as dependency:

$scope.renderHtml = renderHtml;

function renderHtml(str) {
  return $sce.trustAsHtml(str);
}

Solution 2:

Try this and include ng-sanitize

function renderHtml($sce,string) {
    return $sce.trustAsHtml(string);
}

Solution 3:

try this. add ngSanitize depenednecy to app.

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

and also add ngSanitize js file.

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

.controller('ctrl',function($scope, $sce) {
  $scope.html =$sce.trustAsHtml(
     ' this is <br> a <em>test</em>');
  
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular-sanitize.min.js"></script>
<div ng-app="app">
  <div ng-controller="ctrl">
   <p ng-bind-html="html" ></p>
  </div>
</div>

Post a Comment for "AngularJS Parse String To Html"