Parsing HTML from a JSON file causes issues with ngResource functionality

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?

Answer №1

Remember to utilize the controller alias blog when referencing posts for looping purposes.

<div class="post" ng-repeat="post in blog.posts">

Next, include the following code:

<div ng-bind-html="post.description | sanitize"></div>

Important: It seems like there is no ngResource related code included in your question. Did you forget to add a crucial piece of information?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

While the file does exist in the current directory, the `readFileSync` function is unable to locate

For my latest SvelteKit project, I was developing a Joke API using Bun as the runtime and TypeScript as the programming language. The goal of this API was to fetch one random dad joke from a text file that contained 363 jokes. Everything was going smoothly ...

Determine the data type of a string without needing to convert it

Can you determine if a value is numeric without casting it? For example, how can you differentiate between 'abc' and '123' without converting them to a specific data type? While visually apparent that 'abc' is not numeric, t ...

ASP.NET MVC querystring parameter mismatch detected

Having an issue with my querystring - I am calling a controller action using JavaScript in the following way: if ((event >= 65) && (event <= 90) || (event >= 48) && (event <= 57)) { var focussed = document.activeElement.id; ...

I can't figure out why I'm receiving a TypeError stating that onSuccess is not a function within my AngularJS service

Utilizing an angularjs service that utilizes restangular for API calls. angular.module('app.userService', []) .factory('userService', ['localStorageService', '$rootScope', 'Restangular', func ...

Error: "openerp" is undefined in version 10 of Odoo

I encountered an issue while trying to install a module on Odoo version 10. The JavaScript file begins with this code snippet: openerp.my_module_name = function(instance) { console.log('Something'); } When I checked the browser conso ...

Click on the button to create a link and then seamlessly navigate there using a combination of ajax, php, and javascript

I am in need of a button that can trigger a php page through ajax. The purpose of this button is to open the client's email with a mailto link. The php page created generates an email containing an encrypted string that carries credentials for access ...

Having difficulty retrieving JSON data using Jquery

After receiving a JSON string as a response from the server, I encountered an issue when trying to access the objects within the response, only to receive an "undefined" message. Here is the AJAX request being made: $.ajax({ url: 'somefi ...

What is the best way to organize these checkboxes using BootstrapVue's layout and grid system?

My BootstrapVue table setup looks like this: https://i.sstatic.net/K3Rwy.png This is the code for the table: window.onload = () => { new Vue({ el: '#app', computed: { visibleFields() { return this.fields.filter(field ...

"Combining the power of BootstrapDialog and angularJS to enhance your

I am currently utilizing a library called Bootstrap3-dialog from https://github.com/nakupanda/bootstrap3-dialog in order to display dialogs within my web application. $scope.importFileWithSettings = function(){ BootstrapDialog.show({ ...

send a parameter to a bootstrap dialog box

It seems like the title of my question is pretty common, but I haven't been able to find a solution that works for my specific issue. I have a Bootstrap template and what I'm trying to do is pass some variables to a modal. Currently, I am using ...

Interactable elements on a spherical map in three.js

I've been trying to implement raycasting to make objects clickable. Despite referencing numerous examples, the code doesn't seem to work for me. The only notable difference is that I'm working within a sphere. My initial setup involves defi ...

What is the best way to display the HTML content being received from the database in the DOM?

I have received an object in this format [ { "BET": 57630343, "CUSTOMER": 181645, "SPORT": "MLB", "XX_FILL OPEN": "<button class=\"btn\" onclick=\"fillOpen(57630343)\">Fill Open</button>", "XX_VIEW": n ...

Ways to retrieve the initial key object received through an AJAX request

I have a form with three dynamic dropdowns. The second dropdown depends on the first one, and the third dropdown depends on the second one. Therefore, selecting an option in the first dropdown will automatically populate the next two dropdowns. Below is t ...

View the picture directly on this page

Currently, I am in the process of creating a gallery and I would like the images to open on top of everything in their original size when clicked by the user. My expertise lies in HTML and CSS at the moment, but I am open to learning JavaScript and jQuery ...

Customize URL based on selected button

My question may be a bit unclear, but I want to generate dynamic URLs that redirect users to specific pages based on the link clicked. For example: Parent page with links (a, b, c, x, y) User clicks on 'b' User is taken to a Node Page that play ...

Adding a new field to an embedded document in MongoDB (without using an array)

My task involves updating a json document in mongodb by single field. Despite the simplicity if my data was in an object array, it's more complex due to objects within objects caused by early design choices and reasons. This is the structure of the s ...

I encountered an unexpected issue when attempting to retrieve my TAG in Prisma, resulting in an undefined value

Currently diving into the world of CRUD operations in NextJS using TypeScript and Prisma with TanstackQuery. I am facing an issue where I am trying to display the tag.name, but unfortunately, I keep encountering an undefined error when attempting to fetch ...

What is the best way to extract only the "text" field from a JSON server response?

When I receive a server response in JSON format, I want to extract only the "text" field and store it in an arraylist. However, my current code is only retrieving the first text field from the output. How can I modify it to capture all the text fields with ...

Issue with JavaScript executor: receiving an "Unexpected identifier" error when passing a single quote character

I am trying to pass the value below in a script using the Javascript executor. String value = "ac.saveDocket('CompanyRegistration','https://yyy.yyy',0);" JavascriptExecutor executor = ((driver) as JavascriptExecutor) WebEleme ...

Node.js is a versatile platform that allows users to efficiently download zip files into a folder and extract its contents. Discovering the optimal time to commence unzipping the file can be

Looking to create two distinct components - first, a downloader that can take either a file path or a FILE DESCRIPTOR and a URL, then proceed to download the file from that URL into the specified path. To view the code I've developed for the downloade ...