Using the key value from a JSON object of arrays of objects as HTML content in Angular during iteration

Here is the JSON schema I am working with. My goal is to display links within each object's description in my application:

{
  "notes": {
    "long random ID number 1": [
     {
        "name": "test 1",
        "description": "<a href='http://google.com'>google</a>"
     }
    ],
    "long random ID number 2": [
      //more objects with names and descriptions
    ]
  }
}

Within the 'notes' structure, there are multiple arrays of JavaScript objects each with a unique key format. How can I programmatically iterate over these arrays regardless of their keys, then utilize Angular's $sce.trustAsHtml() function on the link contained within each object's description for proper UI rendering?

Edit:

var notesKeys = Object.keys($scope.requirements.notes);

for(var key in notesKeys)
{
    for(var note in $scope.requirements.notes[key])
    {
        $sce.trustAsHtml(note.description);
        $sce.trustAsHtml(note.name);

        console.log(note.description);
        console.log(note.name);
    }
}

Answer №1

Perhaps a solution similar to this could be effective:

$scope.urls = [];
//Retrieve the keys from your hash, with 'object' being your complete object
var notesKey = Object.keys(object.notes)
//Iterate through the keys in notes
for(var i=0; i < notesKey.length; i++){
  // Iterate over each object
  var notes = object.notes[notesKey[i]];
  for (var j=0; j < notes.length ; j++){
   //Access your object and perform necessary actions
   $scope.urls.push($sce.trustAsHtml(notes[j].description));
  }
}

Incorporate ng-repeat and ng-bind-html into your HTML based on your requirements:

<div ng-repeat="url in urls" ng-bind-html="url"></div>

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

Updating the value within a nested array in a ReactJS application

Check out the working code here: https://codesandbox.io/s/broken-https-2i454?file=/src/App.js I'm currently using Material UI within my reactjs project and facing an issue updating the value entered in a textfield of a table using the onChange functi ...

A beginner's guide to integrating ng-class into a directive template using AngularJS

I have been attempting to achieve something similar to the following: template: "<div ng-if=\"successData\" ng-class=\"{ 'bounceInDown': successData == true,bounceInDown: successData == false}\" style=\"border-radius ...

Creating a dynamic step animation with jQuery: A step-by-step guide

Struggling to find resources on how to animate a color wheel using jQuery? Want to create a spiraling effect by gradually revealing colors at 45-degree intervals, like a loading GIF spiral? I need it to cycle through the defined colors in order and then cl ...

Exporting a collection in mongodb using the shell command

I am looking to export a collection from MongoDB using a shell command. However, I have run into an issue with fields containing ":" (such as number:phone and number:fax) not being exported when using the following command: mongoexport --csv -d schemaNa ...

Developing a robust Node.js application with a plethora of APIs

In my current project, we are using Express and Node.js for the framework. Our codebase contains numerous APIs that interact with third-party software, requiring authentication. The challenge we're facing is that we need to provide a client with an ex ...

Using Android's JSONObject to populate a ListView

Having a bit of trouble here, as I am completely new to this. What I am attempting to do is pass a JSONObject to my adapter and have it generate all the rows for that ListView. This is the code I have so far: private static final String TAG_OS = "an ...

Changing an Array into JSON format using AngularJS

I am attempting to switch from a dropdown to a multiselect dropdown. <select name="molecularMethod" class="form-control" ng-model="request.molecularMethod" multiple> It is functioning properly. However, when items are selected, it generates an arra ...

The text-center alignment in Bootstrap doesn't seem to be applying to buttons

After spending a considerable amount of time trying to center two buttons in Bootstrap, I came across the "text-center" class offered by Bootstrap. However, no matter where I include this class, it doesn't seem to have any effect on the alignment of t ...

Display the datepicker beneath the input field

I successfully integrated the datepicker, but I prefer for the calendar to display below the date input field rather than above it. HTML5 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv=" ...

Loading CSS files conditionally in Angular2's index.html

Currently, my index.html page features a dark theme: <base href="/"> <html> <head> <title>XXX</title> </head> <body> <link rel="stylesheet" type="text/css" href="assets/dark_room.css"> <my-app ...

Blending conditional and non-conditional styles in VueJS

Is it possible to set a class in Vue based on the value of a parameter and conditionally add another class if that parameter meets a certain condition? Can these two functionalities be combined into one class assignment? <button :class="'btn btn-p ...

The issue arises when each child within a list lacks a distinct key property

Hello, I am encountering a minor issue. I would like to create a product details page where all the information about the product is displayed. This can be done by clicking on the product link. However, I am facing the following error: "Each child in a lis ...

How can I effectively develop a versatile user interface for a website using Ruby on Rails that is compatible with all

Currently, I am in the midst of developing a web application using Ruby on Rails. Occasionally, I encounter challenges with cross-browser compatibility when it comes to CSS and Javascript. Are there any strategies you recommend to reduce these issues dur ...

Using AngularJS to showcase nested JSON arrays

Just starting out with angularjs and seeking some guidance. Our angular front-end is connected to a REST API delivering JSON data like this: [ { "category_id":"1", "category_name":"dog", "subcategories":[ { ...

React component that enables radio inputs to repeat upon selection

My current project involves creating a quiz app where users can answer single questions using React on Codepen. I am utilizing an API to fetch a question, along with 3 incorrect answers and 1 correct answer, then storing them in the app's state. Howev ...

Galaxy S3 JSON Parsing Problem

I have been developing a small application that retrieves the current geographical coordinates and returns the corresponding address using json objects. I have encountered an issue where the app works perfectly on Samsung Galaxy S, but encounters a runtime ...

Delete the child node that matches the specified variable

I am facing the challenge of removing a list item node if it matches a specific variable. For example If username is equal to User 1 And if a node is also User 1 Then I want to remove this particular node. Unfortunately, I have not been able to assign ID ...

The update event is not triggered in the KendoUI kendo-grid when using a k-detail-template

I've encountered an issue with a kendo-grid that uses k-detail-template as the row structure. The version of KendoUI being used is v2014.3.1411 (Kendo UI Q3 2014 SP2) Below is the HTML code snippet: <div kendo-grid="mainGrid" id="mobileRole" k-op ...

Warning: multiple selections have been made on the checkboxes

Currently, I am using FormData to submit data and trying to alert the values of checked checkboxes in my form. At the moment, I can only make it work for one checkbox. How can I alert the values of all checked checkboxes? var formData = new FormData(docu ...

Tips for updating HTML Ajax content when the back button is clicked

I created a search page with filters that update the URL parameters to prevent values from being lost if the page is refreshed. q = $('#form input[name=q]').val(), srchtype= $('#filter input[name=srchtype]:checked').val(), sortBy ...