1

I have an ionic & angular based project. I get variable node from a service. node's body value is HTML. But when I print {{node.body.und[0].value}} with ionic, it prints HTML codes with tags like <ul><li><strong>Title:</strong>&nbsp;some text</li><ul>

Controller and service part of the code:

.controller('NodeCtrl', function($scope, $stateParams, Node) {
    $scope.node = Node.get({nid: $stateParams.nid});
});

.factory('Node', function ($resource) {
    return $resource('some-domain.com/api/node/:nid'+'.json');
})

Ionic template:

<ion-view view-title="{{node.title}}">
  <ion-content>
    <div class="list card">
      <div class="item">
        <h2>{{node.title}}</h2>
      </div>
      <div class="item item-body">
        {{node.body.und[0].value}}
      </div>
    </div>
  </ion-content>
</ion-view>

How can I make ionic print {{node.body.und[0].value}} as HTML instead of with HTML tags like <ul><li><strong>Title:</strong>&nbsp;some text</li><ul>?

example

5
  • "How can I make ionic print {{node.body.und[0].value}} as HTML instead of with HTML tags like <ul><li><strong>Title:</strong>&nbsp;some text</li><ul>" ... I am assuming you mean how can you get it to print as text? Because it is printing it as HTML if you get the tags. Commented Oct 28, 2015 at 16:49
  • Or is it just inserting the HTML as a string? Commented Oct 28, 2015 at 16:53
  • Have you looked at ng-bind-html ? docs.angularjs.org/api/ng/directive/ngBindHtml Commented Oct 28, 2015 at 16:56
  • I have attached a picture to explain the situation better. Commented Oct 28, 2015 at 16:58
  • I have changed it as <p ng-bind-html="node.body.und[0].value"></p> and it worked as I wanted. Thanks Commented Oct 28, 2015 at 17:28

1 Answer 1

3

All Credits to Will.Harris!!

ng-bind-html, is what you are looking for.

From the Docs, use the ng-bind-html in the view as:

<div ng-controller="ExampleController">
 <p ng-bind-html="myHTML"></p>
</div>

and in the controller, assign the scope variable with the html content as:

angular.module('bindHtmlExample', ['ngSanitize'])
.controller('ExampleController', ['$scope', function($scope) {
  $scope.myHTML =
     'I am an <code>HTML</code>string with ' +
     '<a href="#">links!</a> and other <em>stuff</em>';
}]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.