Ng-Bind-Html-1

 

This is very simple example of the ng-bind-html directive. In this example we used html tags in the $scope.myHTML variables's value, as the html word is in bold tag and the word url is anchor tag. We bind the $scope.myHTML variable with the ng-bind-html directive. This directive makes the html word bold and url as a anchor tag. ngSanitize dependency is required to work with ng-bind-html directive and to get output as html you need to make the html as trusted html. See the example.

 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular-sanitize.js"></script> </head> <body ng-app="myApp"> <div ng-controller="myController"> <any ng-bind-html="myHTML"></any> </div> <script> var app = angular.module("myApp", ['ngSanitize']); app.controller('myController', ['$scope','$sce', function ($scope,$sce) { $scope.myHTML = $sce.trustAsHtml('This is an <b>html </b>string with ' + '<a href="">url!</a>'); }]); </script> </body> </html>
Output