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

Activating the delete event trigger for a tinyMCE object

How can I create a function that activates when a user deletes an object in tinyMCE? I want to remove uploaded images from a cache folder whenever a user deletes an image from the tinyMCE editor. If they upload an image and then delete it, I would like an ...

Error encountered when accessing Spotify API. The requested action requires proper permissions which are currently missing. Issue arises when attempting to

I am attempting to use the spotify-web-api-node library to play a track on my application const playSong = async () => { // Verify access token with console.log(spotifyApi.getAccessToken()) setCurrentTrackId(track.track.id); setIsPlay ...

PHP is encountering difficulties when attempting to fetch extensive requests

I have developed a PHP service that retrieves JSON data from a table consisting of 2100 rows. My development setup includes: Xampp with Apache and MySQL Windows 10 PHP-7.0.8 I am using PDO in PHP to execute the query. The service used to function flawl ...

Is there a secure way to prevent user input from causing issues in a BigQuery node insert? Can the BigQuery.insert node library support parameterized queries for added security

I am seeking advice on how to securely insert user form data into BigQuery using the Google Cloud BigQuery library. Specifically, I am curious about the most effective methods for sanitizing, escaping, and cleaning the input data. Is it feasible to implem ...

Trouble with Sound during Quickblox Webrtc Audio Calls

I am having an issue with my audio calls. When I make a call to another user, everything seems fine except that I cannot hear any sound when speaking into the microphone. I am only interested in making audio calls. The call is initiated by pressing a butt ...

Ways to verify a correct email address using ReactJS

I'm currently working on a project using React.js and Next.js. I'm encountering an issue with handling the Axios response in Next.js as it's displaying "[object Object]" instead of the actual response data. How can I properly handle the resp ...

Having trouble setting the state of an array using NextJS useState?

I'm facing an issue where the array saved to a useState state is not updating properly. Despite properly returning values for the variable first, the variable "data" remains an empty array. Interestingly, adding a console.log("restart") statement und ...

Combine AWS API Gateway with Lambda functions and the RequestHandler for a powerful and flexible interface

When utilizing AWS API Gateway with the integration type set to lambda function, is it feasible in a Java implementation of the lambda function to overwrite RequestHandler (rather than RequestStreamHandler)? This way, the input for my lambda function would ...

Is it possible to run my JavaScript script immediately following the execution of npm run build?

Hey everyone! I am trying to run my JavaScript file right after receiving the successful message from npm run build. Currently, I am working on a Vue.js codebase. Is there a way for me to log and create a file in my code repository containing the 'run ...

HighStock chart malfunctioning with inaccurate epoch datetime display

I am working on a project that involves creating a dynamic Highstock chart to showcase the daily influx of emails. The data is stored in a JSON file that gets updated every day, and you can see a snippet of it below: [{ "name": "Month", "data": [147199320 ...

What's the issue with my jQuery AJAX script?

I am experiencing an issue with my ajax pages using jQuery to retrieve content. Only one page seems to be working properly, even though I have multiple pages set up. How can I resolve this problem? $(document).ready(function() { $('.lazy_content& ...

Identifying keystrokes and triggering audio in Vue.js

Utilizing vue.js, the code snippet provided enables sound playback upon each button click. I am curious about how one can detect a keyboard press event to play a sound when the DOM is ready rather than waiting for button clicks. For instance, triggering ...

Bringing in the component's individual module

I encountered the error message in my Angular application - Can't bind to 'formGroup' since it isn't a known property of 'form' - and managed to resolve it by including the import import { AddEditModule } from './add.edit ...

Utilize $.get AJAX to extract data from a multidimensional JSON array

Struggling to make two functions work on my form that uses AJAX and jQuery to look up an array. One function is functional while the other seems to be causing confusion with over-analysis and extensive troubleshooting. Any insights into what may be missing ...

Converting PHP to JSON data format

I am currently in the process of developing a module for WHMCS. The expected input format is: {"verifyConn": 1} However, the API response I receive looks like this: { "verifyConn":1 } Unfortunately, the module is unable to process ...

Transforming dates from JSON and showcasing them on a DataTable

I'm facing an issue with my local JSON file where the dates are being rendered in a DataTable as /Date(1350028800000)/ (10/12/2012). I came across this thread which talks about converting the date format, but I need help figuring out how to implement ...

Creating an AngularJs input that requires alphanumeric characters

I am currently working on a form with an ID input field. To ensure that it only contains alphanumeric characters without any spaces, I implemented the following code: self.pattern = /^[a-z0-9]*$/; This pattern is utilized in my view as shown below: < ...

What is the proper way to enable the Google Analytics cookie only once another consent cookie has been set and is marked as "true"?

I am currently using React/Nextjs on my website along with the react-cookie-consent library. This setup generates a pop-up where users can agree to the cookie policy, and if they do, a CookieConsent with the value "true" is set. In addition to this, I wis ...

Problem importing npm module with Meteor 1.3

I've been trying to follow the guide for using npm packages in Meteor 1.3, particularly focusing on installing the moment npm module. However, I can't seem to work it out despite its simplicity. Every time I attempt to use the package in the cli ...

Error Encountered: Yii2 Mishap with Ajax Calls

Retrieve the URL creation code $ajaxSaveTestGrid = Yii::$app->urlManager->createUrl('testgrid/ajaxsave'); Here is my AJAX view code function saveRow(id, index) { if(id == 'tbl_testGrid') { save(i ...