Friday, May 11, 2018

binding html to an element in AngularJS using ngBindHtml

1 comment


Sometimes you just have to insert some HTML content returned from any source into your Angular SPA . The ngBindHtml directive evaluates the expression and inserts the resulting html into an element .

To use this functionality we need to include the ngSanitize module as a dependency which is available in "angular-sanitize.js" and has to be included in your application .Add ngSanitize in the module dependencies:

var app = angular.module('app',['ngSanitize'])
           .....

Include angular-sanitize.js in your application:
    ...
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="//code.angularjs.org/snapshot/angular-sanitize.js"></script>
       ....

Bind html:
<h4>Binding html to a div in Angular</h4>
        <p ng-bind-html="html_expression"></p>
             ......

Below is the demo


1 comment :