4thSpace
4thSpace

Reputation: 44312

How to use ng-bind-html?

Any ideas why this bind isn't working?

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

function MyCtrl($scope) {
  $scope.myHTML = "<a href='#'>a link</a>";
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-controller="MyCtrl">
  <p ng-bind-html="myHTML"></p>
</div>

Upvotes: 7

Views: 13170

Answers (1)

Shomz
Shomz

Reputation: 37691

For the HTML binding to work, your module needs to have ngSanitize injected as well as the file angular-sanitize(.min).js included.

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

function MyCtrl($scope) {
  $scope.myHTML = "<a href='#'>a link</a>";
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-sanitize.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
  <p ng-bind-html="myHTML"></p>
</div>

Or you can use String Contextual Escaping (which will probably be the way to do it in future versions of Angular).

Upvotes: 11

Related Questions