In this article I will explain with an example, how to use trustAsHtml method of $sce service for displaying HTML content using AngularJS.
Display HTML in AngularJS using
$sce service
The below HTML Markup consists of an HTML DIV to which ng-app and ng-controller AngularJS directives have been assigned.
Note: Unless the HTML content is trusted using the $sce service, it will not be displayed using ng-bind-html directive.
The AngularJS app HTML DIV consists of an HTML SPAN specified with ng-bind-html directive, which will be used to display HTML content.
<html>
<head>
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $sce) {
$scope.Message = $sce.trustAsHtml("My name is <span style = 'color:red'><b>Mudassar Khan</b></span>");
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
<span ng-bind-html = "Message"></span>
</div>
</body>
</html>
Screenshot
Demo
Downloads