If you want to insert HTML into view from AngularJS controller you can use ng-bind-html
in the HTML. For Angular 1.x, use ng-bind-html
in the HTML:
1 | <span class="pun"><</span><span class="pln">div ng</span><span class="pun">-</span><span class="pln">bind</span><span class="pun">-</span><span class="pln">html</span><span class="pun">=</span><span class="str">"thisCanBeusedInsideNgBindHtml"</span><span class="pun">></</span><span class="pln">div</span><span class="pun">></span> |
At this point you would get a attempting to use an unsafe value in a safe context
error so you need to either use ngSanitize or $sce to resolve that.
Use $sce.trustAsHtml()
in the controller to convert the html string.
1 | <span class="pln"> $scope</span><span class="pun">.</span><span class="pln">thisCanBeusedInsideNgBindHtml </span><span class="pun">=</span><span class="pln"> $sce</span><span class="pun">.</span><span class="pln">trustAsHtml</span><span class="pun">(</span><span class="pln">someHtmlVar</span><span class="pun">);</span> |
There are 2 steps:
<script src="lib/angular/angular-sanitize.min.js"></script>
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngSanitize'])
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.