Why is ng-bind-html not displaying anything?

Edward Tanguay picture Edward Tanguay · Jan 10, 2015 · Viewed 28.6k times · Source

I am displaying a string that has HTML code in it:

<div style="font-size: 14px" ng-bind="currentBook.description"></div>

put it displays the HTML code instead of interpreting the elements:

enter image description here

When I use ng-bind and ng-bind-unsafe, it shows nothing.

How can I get the HTML to be parsed?

Addendum

I added a reference to sanitize but ng-bind and ng-bind-unsafe still show nothing:

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-route.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular-sanitize.js"></script>

Ok, I added the ngSanitize var app = angular.module('app', ['ngSanitize']); and now it works.

Answer

sylwester picture sylwester · Jan 10, 2015

It looks like you missed ngSanitize please see demo below

You can find more here

https://docs.angularjs.org/api/ngSanitize/service/$sanitize

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

app.controller('firstCtrl', function($scope, $sce) {
  $scope.currentBook = {
    description: "<p>some description</p>"

  };
});
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular-sanitize.js"></script>

<body ng-app="app">
  <div ng-controller="firstCtrl">
    <div style="font-size: 14px" ng-bind-html="currentBook.description"></div>
  </div>
</body>