107

Edit: a clarification for anyone who only skimmed the title, my question is about Angular 2, not 1.


I have a component template that is something like this:

<div>{{ post.body }}</div>

The object is something like:

{
    "title": "Some Title",
    "body": "<p>The <em>post body</em>.</p>"
}

Instead of rendering the paragraph like:

The post body

it displays:

"<p>The <em>post body</em>.</p>"

Since it's such a common task, I looked for a built-in pipe like {{ post.body | safe }} but didn't see one.

Is there is an easy way to get that working? Is there a safe way to get that working?

11mb
  • 1,339
  • 2
  • 16
  • 33
R891
  • 2,550
  • 4
  • 18
  • 30
  • What are you using to add the json into your page? – Phiter Jan 21 '16 at 23:04
  • please see: http://stackoverflow.com/questions/17826758/angularjs-render-html-tags-that-are-contained-in-a-string and/or http://stackoverflow.com/questions/23747474/how-to-show-html-in-angularjs-template-instead-of-string – Michael Stilson Jan 21 '16 at 23:09
  • or http://stackoverflow.com/questions/20436579/how-to-have-angularjs-output-escaped-html, or http://www.google.com – Langley Jan 21 '16 at 23:11
  • 9
    `
    ` for Angular2...
    – Sasxa Jan 21 '16 at 23:12
  • @PhiterFernandes - the JSON is from a service that obtains it from mock data at the moment, similar to how it's done on the angular.io tutorials. – R891 Jan 22 '16 at 00:55
  • That one is a duplicate of this question. – R891 Feb 06 '17 at 11:14

1 Answers1

220

In Angular2 you can use property binding to access properties of DOM elements, in your case:

<div [innerHTML]="post.body"></div>
Sasxa
  • 40,334
  • 16
  • 88
  • 102