I am attempting to retrieve blog posts from a JSON file and parse the 'description' in HTML format. Despite using ngResource, I am not receiving any results.
Here is a snippet of sample data from the JSON:
jsonFeed({
"title": "My Blog",
"description": "",
"modified": "2016-05-10T21:21:46Z",
"items": [
{
"title": "Title1",
"description": "<p><a href=\"#">Paul</a> posted a photo:</p> <img src=\"https://farm8.staticflickr.com/7365/26872407641_cfbb210ee7_m.jpg\"/>"
},
{
"title": "Title2",
"description": " <p><a href=\"#">Beth</a> posted a photo:</p><img src=\"https://farm8.staticflickr.com/7287/26333398074_cfbce73532_m.jpg\" />"
}
]
})
Here's the code snippet from app.js:
var app = angular.module('blogApp',['ngResource']);
app.filter("sanitize", ['$sce', function($sce) {
return function(htmlCode){
return $sce.trustAsHtml(htmlCode);
}
}]);
app.controller('BlogController', ['$http', '$scope', function($http, $scope){
var blog = this;
blog.posts = {};
$http.jsonp('url').success(function(data){
});
jsonFeed = function(data){
$scope.posts = data.items;
}
And here's a snippet from index.html:
<body ng-app="blogApp">
<div ng-controller="BlogController as blog">
<div class="post" ng-repeat="post in posts">
<h2>{{post.title}}</h2>
<div ng-bind-html="'{{post.description}}' | sanitize"></div>
</div>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-resource.js"></script>
<script src="app.js" type="text/javascript"></script>
</body>
Despite my efforts, all I see in the result is "{{post.description}}". I have even tried inspecting the element in console:
<div ng-bind-html="'<p><a href="#">Paul</a> posted a photo:</p> <img src=\"https://farm8.staticflickr.com/7365/26872407641_cfbb210ee7_m.jpg\"/>' | sanitize" class="ng-binding">{{post.description}}</div>
Why am I unable to successfully parse and display the HTML content of the description?