Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ng-bind-html?

Tags:

angularjs

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>
like image 442
4thSpace Avatar asked Dec 13 '14 22:12

4thSpace


1 Answers

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).

like image 197
Shomz Avatar answered Sep 20 '22 12:09

Shomz