AngularJS | ng-bind-html Directive
The ng-bind-html Directive in AngularJS is used to bind the innerHTML of an HTML element to application data, and remove dangerous code from the HTML string. $sanitize service is must for ng-bind-html directive. It is supported by all HTML elements.
Syntax:
<element ng-bind-html="expression"> Contents... </element>
Example: This example illustrates the ng-bind-html Directive.
<!DOCTYPE html><html ng-app="myApp"> <head> <title>ng-bind-html Directive</title> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"> </script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-sanitize.min.js"> </script> <style> .green { color: green; font-size: 20px; } </style></head> <body ng-controller="geek" style="text-align:center"> <h1 style="color:green;">GeeksforGeeks</h1> <h2 style="">ng-bind-html Directive</h2> <p ng-bind-html="text"></p> <script> var myApp = angular.module("myApp", ['ngSanitize']); myApp.controller("geek", ["$scope", function($scope) { $scope.text = "<span class='green'> GeeksforGeeks</span> is the" + " computer science portal for geeks."; }]); </script></body> </html> |
Output:


