Why is ng-bind-html not displaying anything?

All we need is an easy explanation of the problem, so here it is.

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?


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

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-route.js"></script>
<script src="https://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.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

It looks like you missed ngSanitize please see demo below

You can find more here


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>

Method 2

call apply

    $scope.$apply(function () {
            $scope.currentBook.description = $sce.trustAsHtml(html);

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply