In this post, we will learn how to bind HTML elements in Angular.js. Here, I have added the full source code for this. First, we need to add two JS files - angular.js and angular-sanitize for binding the HTML. For this example, we have created the HTML page and added the below source code for displaying HTML in the p tag.
First, we need to add the JavaScript file for accessing the AngularJS function.
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
- <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-sanitize.js"></script>
Write the below code for displaying HTML in p tag.
- <!DOCTYPE html>
- <html>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
- <script src="https://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="Text"></p>
- </div>
- <script>
- var app = angular.module("myApp", ['ngSanitize']);
- app.controller("myCtrl", function($scope) {
- $scope.Text = "<h1>Display HTML in H1 tag</h1>";
- });
- </script>
- </body>
-
- </html>
The above code displays h1 tag inside p tag using ng-bind-html.