Inserting HTML into View from AngularJS Controller
Inserting HTML fragments created in AngularJS controllers into the view can be challenging due to Angular's security measures, which prevent direct HTML rendering. To overcome this, utilize ng-bind-html in HTML:
<div ng-bind-html="thisCanBeusedInsideNgBindHtml"></div>
This will prompt a security error that can be resolved using ngSanitize or $sce.
Using $sce:
In the controller, convert the HTML string using $sce.trustAsHtml():
$scope.thisCanBeusedInsideNgBindHtml = $sce.trustAsHtml(someHtmlVar);
Using ngSanitize:
<script src="lib/angular/angular-sanitize.min.js"></script>
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngSanitize'])
The above is the detailed content of How to Safely Insert HTML into AngularJS Views from Controllers?. For more information, please follow other related articles on the PHP Chinese website!