AngularJS ng-bind-html Directive

Definition and Usage

ng-bind-html Directives are a safe way to bind content to HTML elements.

When allowing AngularJS to write HTML in your application, you should check for dangerous code in the HTML. By including the "angular-sanitize.js" module in your application, you can use the ngSanitize function to check HTML code.

Example

Bind the innerHTML of the <p> element to the variable myText:

<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-sanitize.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
    <p ng-bind-html="myText"></p>
</div>
<script>
var app = angular.module("myApp", ['ngSanitize']);
app.controller("myCtrl", function($scope) {
    $scope.myText = "My name is: <h1>Bill Gates</h1>";
});
</script>

Try It Yourself

Syntax

<element ng-bind-html="expression</element>

All HTML elements are supported.

Parameters

Parameters Description
expression Specify the variable or expression to be calculated.