4

I am trying to render a text as html using ng-bind as the docs show

<div ng-bind-html="text"></div>

The problem is that Angular removes the style attribute and renders this:

"<mark style='background-color:#FF9DFF;'>APPLE</mark>."

to this:

<mark>APPLE</mark>

How to render as html and keep the styles? I am using Angular version 1.2.6

1
  • just be reminded $sce is v1.2 and above. Commented Mar 14, 2014 at 15:10

2 Answers 2

4

You may try this function when you're doing ng-bind-html in your controller

$sce.trustAsHtml('myHTML'); //$sce would be the parameter with $scope in module

Hope this will work

$NgSanitizeDocs

Sign up to request clarification or add additional context in comments.

Comments

2

First of all include angular-sanitize.js

<div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div>

Then in the controller, add this method

pk.controller("createBlog", function($scope, $sce){
   //ace needs to be injected in the controller.
   $scope.deliberatelyTrustDangerousSnippet = function() {
       return $sce.trustAsHtml($scope.htmlcontent); //html content is th binded content.
   };
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.