Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 945
Edward Tanguay Avatar asked Jan 10 '15 20:01

Edward Tanguay


2 Answers

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>
like image 138
sylwester Avatar answered Sep 27 '22 22:09

sylwester


call apply

    $scope.$apply(function () {
            $scope.currentBook.description = $sce.trustAsHtml(html);
        });
like image 37
Paratoner Avatar answered Sep 27 '22 23:09

Paratoner