Looking to update the display of an array received in JSON format and then show it using ng-repeat?

Upon receiving a response from the backend, I am saving it in a variable called "allinfo" in my controller. The data received includes the name, date of birth, and hobby of a person. The hobby is an array that can contain any number of elements.

In my view, I applied an ng-repeat over all the info stored in "allinfo" and for repeating hobbies, I used another ng-repeat to iterate over each hobby in the "info.hobby" array.

The issue I'm encountering is that when I repeat the info in "allinfo" in my view, it displays the name and DOB correctly but doesn't show the hobbies until I refresh the page.

After looking at the console log, I realized that the Hobby object is indeed received in "allinfo," but initially as an empty array. Only after refreshing does the array populate with all the necessary elements.

If anyone has encountered this issue before and knows how to solve it, please help me out. Thank you in advance!

Answer №1

Give this a try, it should meet your expectations.

In the code below, I am assuming that the responseData variable contains data in the same format as it is received from the server.

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.responseData = [
             {
             "name" : "Alpha",
             "dob" : "04-11-1991",
             "hobby" : [["singing"],["writing"]]
             },
             {
             "name" : "Beta",
             "dob" : "04-11-1990",
             "hobby" : [["playing"],["making friends"]]
             }
    ];
    
    var hobbiesData = [];
    for (var i in $scope.responseData) {
      hobbiesData = [];
      for (var j in $scope.responseData[i].hobby) {
        for(var k in $scope.responseData[i].hobby[j])
        hobbiesData.push($scope.responseData[i].hobby[j][k]);
      }
          var hobbies = hobbiesData.join();
          $scope.responseData[i].hobby = hobbies;
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <div ng-repeat="data in responseData">
         Name : {{data.name}}<br>
         Date of Birth : {{data.dob}}<br>
         Hobbies : {{data.hobby}}
  </div>
</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

Difficulty in using window.scrollTo within useEffect in reactJS

I'm facing an issue where I am trying to use window.scrollTo on the initial render using the useEffect function. I have stored the vertical offset as an integer in localStorage, but the scrolling is not working and remains at position 0. Problem with ...

Accessing various sections and properties of a JSON file using C# parsing

==========UPDATE After realizing that my initial description was misleading, I apologize for any confusion caused. Let me provide further clarification. I am required to iterate through a JSON file that is not as structured as initially mentioned; it is ...

HTML: Organize a slightly intricate column in a table

I am facing an issue with sorting columns in an HTML table that has 2 headers row: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="styles.css"> </head> <body> <table ...

Issues with jQuery Ajax sending Post data to PHP script

I've been attempting to transfer variables from my JavaScript to a PHP file using AJAX, but for some reason, it's not functioning as expected. Despite researching numerous similar queries, I have yet to come across a solution. Here is an excerpt ...

Exploring the concept of array inheritance in Java

Imagine a scenario where a parent class has a toString() method that returns "parent". Now, if this method is overridden in the child class to return "child" instead. If the child object is placed in an array of Parents and we call toString() without any c ...

Exploring the possibilities of integrating the iTunes Store customer reviews API

Recently, I started working with the iTunes store customer reviews API and it has raised some queries for me. The standard URL format looks like this: https://itunes.apple.com/us/rss/customerreviews/id=(APPID)/sortBy=mostRecent/json One question on my mi ...

Decoding JSON with JavaScript following the response from JsonConvert.SerializeObject(json) in a .NET handler

I am currently working on a web application using the .NET platform. I have written a Handler code that returns a JSON object to JavaScript (after making an AJAX request). Here is the Handler code: var wrapper = new { left = left.ToString(), t ...

Scene three js appears to be stuck in a perpetual state of emptiness, making it challenging to start

I've encountered a major issue with understanding how to use three.js. Despite my best efforts over the past few days, I haven't been able to successfully implement three.js into my projects. Initially, I attempted using Parcel by starting a new ...

Tips for Passing Parameters to Vuex mapActions correctly:Executing mapActions in Vuex requires a specific

I am facing an issue with my ProjectPage.vue where I display project issues in a v-data-table. The projects are fetched from a server API call in the sidebar and displayed there. Once I select a project, I want to use its id to retrieve its corresponding i ...

Why is it that $_GET consistently returns null despite successfully transmitting data via AJAX?

I'm currently developing a PHP script that dynamically generates tables in MySQL based on user input for the number of rows and columns. The data is being sent to the server using AJAX, but even though the transmission is successful, the server is rec ...

Is it possible to open a PDF file in a new tab using Angular 4?

When attempting to open a PDF file using the window.open() function, I encountered an issue where the window would open and close automatically, causing the file to be downloaded rather than displayed in a new tab. Despite not having any ad blockers inst ...

Prevent the upward arrow from appearing on the first row and the downward arrow from appearing on the last row when

This is a straightforward question, but I'm unsure how to achieve it. I am currently working on sorting columns using jQuery. $(document).ready(function(){ $(".up,.down,.top,.bottom").click(function(){ var row = $(this).parents("tr:f ...

Webpack is throwing an error stating that it cannot find a module with the relative path specified

Here is the structure of my app (excluding the node_modules directory): ├── actions.js ├── bundle.js ├── components │   ├── App.js │   ├── Footer.js │   ├── Link.js │   ├── Todo.js │   └─ ...

Conceal a section of a container with the click of a button within a WordPress Plugin

I am currently utilizing a Wordpress plugin known as contact form 7 to construct an email list for an upcoming website project. The client has specifically requested that we avoid using services like mailchimp due to their preference of not sending ANY ema ...

How can I execute a synchronous MongoDB query in Node.js properly?

When utilizing the Node.JS driver for MongoDB, I am interested in executing a synchronous query. Here is an example of what I am aiming to achieve: function retrieveSomething() { var database = new mongo.Db("mydatabase", server, {}); database.ope ...

Is utilizing the correct use case for a Bunyan child logger?

I've recently started exploring bunyan for logging in my nodejs application. After giving it a try, everything seems to be functioning quite smoothly. Initially, I overlooked a section on log.child, but now I am eager to understand its usage. It appea ...

Utilize gson to parse JSON containing a variety of child objects, then return it as a list

I'm currently dealing with an issue involving parsing JSON that contains mixed arrays of child classes, and I need to return it as a Java list to the client. Example JSON: { "status": "OK", "results": [ { "type": "one", "Id": "2 ...

Error encountered while attempting to convert a unique custom object to JSON in Jersey

Before jumping to conclusions and marking this question as a duplicate, I want to clarify that I have already explored the solutions provided in A message body writer for Java class not found javax.ws.rs.WebApplicationException: com.sun.jersey.api.Messag ...

Randomly retrieving Vue data from a JSON source

For my first venture into creating a quiz app with Vue and Tailwind, I want to ensure that the quiz's content appears different each time it is accessed. Currently, I have set up my Vue component to display data from a JSON file in the following manne ...

Ajax is able to fetch a JSON string, however it struggles to iterate through the successful data as an

My current project involves creating a graph using d3.js. I came across some php code that fits my needs perfectly. However, I am working with c# and Visual Studio, so I need to convert it into asp.net. For starters, I want to input some hardcoded sample d ...