orbitory
orbitory

Reputation: 1120

How do I display html coming from a json list in AngularJS?

having a json set of information like

[
    {
    "title":"Title 1",
    "description":"<p>Some HTML</p>"
    },
    {
    "title":"Title 2",
    "description":"<p>Some HTML</p><p>More HTML</p>"
    }
]

how do I show the html in my template. Currently the html is not decoded. I tried with the code below but it doesn't work.

{{item.title}}
<div data-ng-bind-html='item.description'></div>

Controller looks like

var searchApp = angular.module('searchApp', ['ngSanitize']);

searchApp.controller('SearchCtrl', function ($scope, $http) {
  $http.get('search-json').success(function(json) {
    $scope.item = json.data;
    $scope.orderProp = 'title';
  });
});

Thank you

Upvotes: 0

Views: 1314

Answers (1)

harishr
harishr

Reputation: 18055

add ngSanitize , inject $sce and then in js

   $scope.getHtml = function(html) {
       return $sce.trustAsHtml(html)    
   }

and in html

<span>{{item.title}}</span>
<div data-ng-bind-html="getHtml(item.description)"> <div>

Upvotes: 1

Related Questions