0

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

2
  • if json.data is an array of objects you have to write {{item[0].title}} or use ng-repeat Commented Jul 26, 2014 at 14:04
  • That's what I do, problem is item.description comes out as <p>Some HTML</p> instead of the html Commented Jul 26, 2014 at 14:08

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this seems to work but I think that for version 1.3 you don't need the getHtml anymore. Just adding $sce seems to fix the problem for me. Not sure why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.