How to render raw html with AngularJS?
Asked Answered
A

3

15

I'm creating an AngularJS single page application. The data will be fetched from a webservice in json-format.

The problem is that some text elements come with preformatted html tags

json output:

{
   "text": "<p><span style="text-decoration: underline;"><strong>test text</string></span></p>"
}

Now how can I display this text and render the html directly, so that only "test" is shown to the user and the rest serves as markup?

<h1>{{data.text}}</h1>
Anticathode answered 7/8, 2015 at 14:30 Comment(1)
Could this help ? #9382426Ryun
E
21

You need to add ng-bind-html="data.text" to your h1 tag.

Your html would look like:

<h1 ng-bind-html="data.text"></h1>

Documentation: ngBindHtml

Externalization answered 7/8, 2015 at 14:37 Comment(4)
That sounds promising, though when I use it I don't see any output anymore, just a blank element.Anticathode
@Anticathode You seems have formatting issues in your json. Your attribute stule should use ' instead of " else it breaks everything. But it's maybe just a typo there.Badly
That's just a typo in my example here.Anticathode
I was missing the angular-sanitize module, works now.Anticathode
L
6

Update2: is it possible to strip the html for you? It could be done so:

angular.module('myApp.filters', []).
   filter('htmlToPlaintext', function() {
      return function(text) {
         return String(text).replace(/<[^>]+>/gm, '');
      };
   }
);

And you html:

<div>{{myText | htmlToPlaintext}}</div>

See more information: angularjs to output plain text instead of html

Update: do you really need the html from your json? It's better to store your html in the views and get the data from your json. Nice separation and very easy to use.

It's possible, but not so easy as non-html (great security).

In Angular 1.3 you need as follows:

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

In your controller add this:

$scope.htmlBind = $sce.trustAsHtml('<span>Hi, I am <em>Joe</em>');

Explanation: you see the $sce:

$sce is a service that provides Strict Contextual Escaping services to AngularJS.

trustAs(type, value)

Delegates to $sceDelegate.trustAs. As such, returns an object that is trusted by angular for use in specified strict contextual escaping contexts (such as ng-bind-html, ng-include, any src attribute interpolation, any dom event binding attribute interpolation such as for onclick, etc.) that uses the provided value. See * $sce for enabling strict contextual escaping.

Read more here:

Ligamentous answered 7/8, 2015 at 14:39 Comment(4)
Is it possible to resolve this directly within html? Because I'm getting a json response and want to dynamically refer to the elements within html by <h1 ng-bind-html="data.text"></h1> without having to call the $sce from within a controller.Anticathode
I cannot control the json response, and thus I'm forced to process preformatted html markups.Anticathode
No it's not possible because I'm required to show the text with interpreted markup as I get it.Anticathode
This is the best solution for me and only ng-bind-html neededFarber
C
-1

Try to use this https://docs.angularjs.org/api/ng/directive/ngBind

<script>
  angular.module('bindExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.name = 'Whirled';
    }]);
</script>
<div ng-controller="ExampleController">
  <label>Enter name: <input type="text" ng-model="name"></label><br>
  Hello <span ng-bind="name"></span>!
</div>
Connacht answered 7/8, 2015 at 14:37 Comment(1)
He wanted to print html, not a simple var.Badly

© 2022 - 2024 — McMap. All rights reserved.