0

I am trying to render some unsafe HTML (basically a HTML snippet with some inline styling) and have the following code in my view:

<div ng-repeat="snippet in snippets">
  <div ng-bind-html="snippet.content"></div>
</div>

All my styling gets removed...

I've heard of people using ngBindHtmlUnsafe however I couldn't find a reference to it and simply putting ng-bind-html-unsafe doesn't render anything.

Any help would be appreciated.

2
  • What AngularJS version are you using? Commented Dec 13, 2013 at 4:29
  • You need to include the html string you're trying to use also. Commented Dec 13, 2013 at 4:31

2 Answers 2

8

The ng-bind-html-escape was removed in AngularJs 1.2.

To achieve the same effect I would advise you to create a filter to trust the resource (you should include the $sce module ):

app.filter('unsafe', function($sce) {
    return function(val) {
        return $sce.trustAsHtml(val);
    }; });

Usage:

<ELEMENT ng-bind-html="htmlValue | unsafe"></ELEMENT>

You shouldn't forget to include the ngSanitize as the app dependency:

angular.module('app', ['ngSanitize'])

Cheers.

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

2 Comments

@Flek Hmm.. That's interesting. I said that simply because in the docs regarding ng-bind-html it says: "To utilize this functionality, ensure that $sanitize is available, for example, by including ngSanitize in your module's dependencies (not in core Angular.)". But I guess if you have a escaped html you don't need to. Thanks for the info though!
Yes the docs confused me in the beginning as well. I just figured out that ngSanitizeis not needed because I forgot to include it once and it still worked.
6

You can bypass it using $sce.trustAsHtml . See documentation

self.snippet.content = $sce.trustAsHtml('some html');

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.