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;

functionrenderHtml(str) {
  return$sce.trustAsHtml(str);
}

Solution 2:

Try this and include ng-sanitize

functionrenderHtml($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>');
  
})
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular-sanitize.min.js"></script><divng-app="app"><divng-controller="ctrl"><png-bind-html="html" ></p></div></div>

Post a Comment for "Angularjs Parse String To Html"