Is it possible to create an HTML fragment in an AngularJS controller and have this HTML shown in the view?
This comes from a requirement to turn an
For Angular 1.x, use ng-bind-html
in the HTML:
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.
$scope.thisCanBeusedInsideNgBindHtml = $sce.trustAsHtml(someHtmlVar);
There are 2 steps:
include the angular-sanitize.min.js resource, i.e.:
In a js file (controller or usually app.js), include ngSanitize, i.e.:
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngSanitize'])