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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
ngbindhtml



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.