Eval JSON data as HTML?


#1

I have html content that’s being converted into JSON format and then being pulled into my views. But it’s currently printing escaped html (so i’m looking at what’s essentially the “source” in my view).

Is there any quick easy way to eval this as html?


#2

How about a sample for that. If you post something, we can play with it to work it out.


#3

Have you tried using ngSanitize on the html from the incoming JSON ? I agree with @Calendee, a sample would be a great place to begin :wink:


#4

Example HTML in JSON: <h2 id=\"myid\">My Title<\/h2>

Another sample: <a title=\"title\" href=\"http:\/\/www.mydomain.tld\/\">mylink<\/a>

I’ve tried using ng-bind-html-unsafe/ngSanitize but it’s not working in the simplest form.


#5

Could you create a CodePen, Plunker, or JsBin sample of your code? We can’t really see the problem without it.


#6

While I do appreciate the willingness to help on most of my issues; I feel that you’re dismissing everything that doesn’t have a full set of code stripped out into a plunker of some sort. Maybe my english isn’t that great, I’m not entirely sure. I am sorry about that if I’m not explaining the problem in enough detail.

So please let me try to explain better:

I have data binding that takes the value from JSON. The data binding is assigned to {{json.value}}, where value is <a title=\"title\" href=\"http:\/\/www.mydomain.tld\/\">mylink<\/a>

Currently what happens in the view:

<a title="title" href="http:\\www.mydomain.tld\">mylink</a>

Instead of:
mylink

Because the data still contains the escaping (which needs to be sanitized/evaluated as html).

So in my previous post I linked a stack overflow which I had implemented the answer using 'ngResource' and ng-bind-html-unsafe as described by the poster. But it is not good/incomplete answer.

Again, what I have is escaped HTML data in the form of <a title=\"title\" href=\"http:\/\/www.mydomain.tld\/\">mylink<\/a> that needs to be converted/sanitized to <a title="title" href="http:\\www.mydomain.tld\">mylink</a>

I’m not sure how I could plunk that into anything other than how i’ve said it:
<a title=\"title\" href=\"http:\/\/www.mydomain.tld\/\">mylink<\/a>
needs to equal
<a title="title" href="http:\\www.mydomain.tld\">mylink</a>

Sorry again if my explanation isn’t good enough; but I do not have any code other than what I posed above.


#7

I’ve done a code sample for you:

I suggest you read http://docs.angularjs.org/api/ngSanitize/service/$sanitize