Edward Tanguay Edward Tanguay - 6 months ago 23
AngularJS Question

Why is ng-bind-html not displaying anything?

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

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>

Comments