Introduction
In my previous articles I told you about:
- ng-click and ng-blur Directive of AngularJS
- ng-init and ng-repeat Directive of AngularJS
- ng-change Directive of AngularJS
In this article I will tell you about the ng-BindHtml Directive of AngularJS.
Using ng-BindHtml we can declare a HTML element in the Controller but they work similarly to a normal HTML element.
Step 1
First of all you need to add two external Angular.js files to your application, in other words:
- angular.min.js
- angular-sanitize.min.js
For this you can go to the AngularJS official site or can download my source code and then fetch it or you can click on this link and download it: ANGULARJS.
After downloading the External Files now you need to add these files to the Head section of your application.
<head runat="server">
<title></title>
<script src="angular.min.js"></script>
<script src="angular-sanitize.min.js"></script>
</head>
Step 2
Now I will work on the ViewModel or JavaScript part of this application, here I am using the second method for initializing AngularJS (I already told you three ways of initializing AngularJS).
Write this code in the Head Section:
<head runat="server">
<title></title>
<script src="angular.min.js"></script>
<script src="angular-sanitize.min.js"></script>
<script>
angular.module('htmldoc', ['ngSanitize']).controller('x', function ($scope) {
$scope.html = 'You can see that I am <b>Bold </b> and <I>Italic </I> also';
});
</script>
</head>
Here I declared "htmldoc" using the angular.module, this "htmldoc" is the value that will be provided in the ng-app. Then "ngSanitize" is used that is necessary if we are using ng-BindHtml.
Then I have provided the name of the controller as "x", then you can see that I have provided an initial value in which the bold and italic tags are used, in the output you will see that they are working as normal HTML elements work.
Our work on ViewModel is complete and now we just need to work on the View part or design part of this application.
Step 3
Write this code in the Body tag:



Join the conversation! Your thoughts help the community grow.