Rendering HTML as it appears in the database in to a view using AngularJS

446 views Asked by At

I am using TinyMCE to style content that the user then saves to the database, an example of what the user could be saving is as below:

<ol><li><em>This</em> is a <span style="text-decoration: underline;" data-mce-style="text-decoration: underline;">HTML</span> email that <strong><span style="font-family: 'times new roman', times;">should</span></strong> <span style="font-family: tahoma, arial, helvetica, sans-serif;">contain</span> different <span style="font-family: arial, helvetica, sans-serif;">fonts</span> in weights with lists and links...</li></ol>

I am currently using ng-bind-html to display this HTML content in my view, however, it appears to be stripping the inline styles. Here is my source from the browser:

<ol><li><em>This</em> is a <span>HTML</span> email that <strong><span>should</span></strong> <span>contain</span> different <span>fonts</span> in weights with lists and links...</li></ol>

Is there a way in which I can keep these inline styles in the view?

2

There are 2 answers

2
Sudhir Bastakoti On BEST ANSWER

you could try marking your HTML as trusted, passing in $sce, as:

yourApp.controller('someCtrl', ['$scope', '$sce', function($scope, $sce) {      
  $scope.some_html = $sce.trustAsHtml(some_html_content);
}]);
0
alisabzevari On

If you are using Angular 1.2+ you have do this:

$scope.htmlToBind = $sce.trustAsHtml(yourHtml);

See documentation for more details.