Utilize Angular's ng-repeat directive to iterate through this JSON data structure for implementing user-to

Struggling with the user-to-user messaging interface on an app and having difficulty using ng-repeat on the items.

Here is the provided data:

   {
     "ID": 4118,
     "CreatedDate": "2015-08-20T03:12:50.397",
        "recipient": [
         {
           "ID": 13,
           "FirstName": "Heather",
           "LastName": "Martin",
           "ProfileImage": "https://../profilepictures/13"
         }
       ],
 "sender": [
        {
          "ID": 1046,
           "FirstName": "Brad",
           "LastName": "Martin",
           "ProfileImage": "https://../profilepictures/1046"
        }
      ],
  "messages": [
    {
      "ID": 4137,
      "ConversationID": 4118,
      "UserID": 1046,
      "Body": "hey",
      "CreatedDate": "2015-08-20T14:34:42.4716233+00:00"
    }
  ]
}

The goal is to display one <li> element per message in a very simple layout.

                 $scope.convo = JSON.parse(localStorage.getItem("convo-" + $stateParams.chatId));

Desired structure for displaying messages:

  <ul>
        <li class="item-avatar-left" ng-repeat="c in convo track by $index">

            <img ng-src="{{c.recipient[0].ProfileImage}}" />

            <p class="bubble speech">
                {{c.messages[0].Body}}

            </p>

        </li>

    </ul>

Various attempts at using the ng-repeat directive have been made.

Ideally, each message should be displayed as a separate list item (<li>).

Current result: https://i.stack.imgur.com/3dSJN.jpg

Console output of a conversation from LocalStorage: https://i.stack.imgur.com/OHjH2.jpg

Answer №1

One way to achieve this is by utilizing the ng-repeat directive.

Within your controller:

$scope.convo = [
                 {
                      "ID": 4118,
                      "CreatedDate": '2015-08-20T03:12:50.397',
                      //...... as per your data
                 }
               ];

In your HTML:

    <ul>
     <li class="item-avatar-left" ng-repeat="c in convo">

       <img ng-src="{{c.recipient[0].ProfileImage}}" />

       <p class="bubble speech" ng-repeat="m in c.messages">
                            {{m.Body}}

              </p>

      </li>
  </ul>

Note: Make sure that your $scope.convo is an array.

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

Dividing Faces with Lengthy Edges in three.js into Two Separate Faces

I am currently working on a script that involves splitting overly long edges of faces into two separate faces instead of one large face. The final result is exported to a .obj file. Although the geometry reduction works fine, I have noticed some issues a ...

Load a Javascript file from the local server

My website is encountering a problem when trying to load a script from a localhost server. The error message displayed is PROTOCOL ERROR: https://localhost:4200/script/test.js net::ERR_SSL_PROTOCOL_ERROR Here is the code snippet causing the issue: <!DO ...

What is the best way to connect a JavaScript file to an HTML file in an Express app?

I have my NodeJS server set up with Express, utilizing Handlebars as the rendering engine. app.use(express.static(publicDirPath)) (...) app.engine("hbs",hbs({ extname: "hbs", defaultView: "main", layoutsDir: path.join(srcDirP ...

Using jQuery to merge two JSON objects into a single table

I'm currently working on creating a table like this: <table id="list"> <th>ID</th> <th>Name</th> <th>Price</th> <th>Image</th> < ...

Setting up a nodejs application on a web server

As a newcomer to NodeJS, I am currently learning how to create a sample app using this technology. I have successfully tested it on my localhost, but now I am facing issues when trying to host it on a public server. var http = require('http'); ...

Converting a JSON object to a class in ASP.NET MVC - A step-by-step guide

Having trouble converting the JSON object below to a class in Asp.Net MVC. { "Name.Test":"fsafasfda" } If you have any suggestions, they are greatly appreciated. The situation is as follows in an ASP.NET action: [HttpPut] public JsonResult Te ...

Encountering a problem with serializing data.Json, unable to dump the data

import json data={"name":"John","age":30,"cars": [{ "name":"Ford", "models":["Fiesta", "Focus", "Mustang" ] },{ "name":"BMW", "models":[ "320", "X3", "X5"] },{ "name":"Fiat", "models":[ "500", "Panda" ] }]} with open('newjson','w') as ...

Manipulating the InnerHTML content of a total of 144 individual cells

I am currently developing a tile-based game using tables. Each td cell contains the following code: <td> <div id="image" style="display:none; display: fixed; right: 5; top:2;"><img src="http://thumb7.shutterstock.com/display_pic_with_logo ...

Display overlay objects specifically focused around the mouse cursor, regardless of its position on the screen

I am currently working on a project using SVG files and processing.js to develop a unique homepage that incorporates both animation and static elements. The concept is to maintain the overall structure of the original homepage but with varying colors, esse ...

Enhancing transparency with a touch of background color

After successfully exporting my chart created in canvas as an image file, I noticed that the image turned out to be transparent without any background. Is there a way through code to add a background color to this existing image obtained from canvas? For ...

What causes old data to linger in component and how to effectively clear it out

Fetching data from NGXS state involves multiple steps. First, we define the state with a default list and loading indicator: @State<CollectionsStateModel>({ name: 'collections', defaults: { collectionList: [], (...), isListLoading: true, ...

Ensure the CSS class stays on Quill clipboard.dangerouslyPasteHTML

One of the challenges I face with using the Quill Text Editor is that when I use the method clipboard.dangerouslyPasteHTML to paste HTML into the editor, it does not maintain custom CSS classes. For example: let content= '<p class="perso-clas ...

Trouble getting AngularJS $scope arrays to populate with multiple PHP to MySQL queries

In my Angular controller, I used to fetch data from a PHP file that pulled one query from the database, stored it in a scope array, and displayed it on the webpage successfully. However, now I am trying to execute two queries in the same file. Each query ...

Bringing custom JavaScript functions into a Vue.js component

In my Vue.js project, I have an abundance of Javascript processing that is all local and doesn't require server-side functionality. I'm exploring the possibility of importing a file containing specific processing functions (such as mathematical a ...

Issue with Unicode in NetworkX: "&" is transformed into "&"

Hey there, I'm currently working with networkX on colab to convert JSON data to GML format. The issue I'm facing is that networkX is converting the "&" symbol in my JSON into "&amp;" as shown below: for node in data['nodes'][&ap ...

AngularJS hash links are designed to smoothly redirect users to a different webpage URL

I am trying to create a link to an element on the same page. However, when I click the link it redirects me to the same URL with #id attached even though I have html5Mode enabled. Looking for guidance on anchor hash linking in AngularJS Here is my direct ...

Managing State in React using Maps

As someone who is new to coding, I am currently facing an issue and seeking help to resolve it. I am using axios to retrieve a JSON file and store it in a state utilizing Redux for form population. I am then utilizing .map() to break down the array and dis ...

I'm looking for the best way to display an error message in HTML using a custom validation directive that I'm creating

<!DOCTYPE html> <html><!--starting of html--> <script src="angular.js"></script> <body ng-app="myApp"> <p>Feel free to type in the input field:</p> <form name="myForm"> <input name="myInput" type=" ...

Typescript - unexpected behavior when using imported JavaScript types:

I am struggling with headaches trying to integrate an automatically generated JavaScript library into TypeScript... I have packaged the JavaScript library and d.ts file into an npm package, installed the npm package, and the typings modules in the TypeScr ...

Choosing the image that represents your website in Safari web previews

Every time I check iCloud.com on my Safari top sites, the thumbnail is always the same. I'm curious about how I can automate this for my own website. ...