Angular ng-repeat failing to recognize updates

When using the ng-repeat to iterate through a JSON response obtained via http.get(), I encounter a situation where, inside the success() callback, I utilize a for-loop to create a new array that needs to be processed by another ng-repeat.

Here is a snippet of the code:

$http({
   method: "GET",
   url: "xxx",
   headers: {
    "Accept": "application/json;odata=verbose"
   }
   }).success(function (data, status, headers, config) {

     $scope.onboardings = data.d.results;
     var counter = {};

     for (var i = 0; i < $scope.onboardings.length; i += 1) {
           counter[$scope.onboardings[i].Company] = (counter[$scope.onboardings[i].Company] || 0) + 1;
         }

     $scope.onboardingCompanies = counter;
     console.log($scope.onboardingCompanies);
})

Within the HTML part:

<div ng-repeat="item in onboardingCompanies">
    <p>{{item.key}}</p>
    <p>{{item.value}}</p>
</div>

Therefore, I require the ng-repeat to recognize alterations in $scope.onboardingCompanies. It appears there may be some asynchronous issue here.

Inspecting $scope.onboardingCompanies with

console.log($scope.onboardingCompanies)
:

Object {Monjasa A/S (FRC): 35, C-bed BV: 2, Monjasa DMCC: 1, Monjasa Pte: 4, Monjasa SA: 9…}

Any guidance on how to approach this?

Answer №1

Your HTML code needs to be adjusted: When using Angular, you will not receive an object with both key and value attributes together. Instead, they are provided separately:

<div ng-repeat="(key,value) in onboardingCompanies">
    <p>{{key}}</p>
    <p>{{value}}</p>
</div>

If you use the form

ng-repeat="item in onboardingCompanies"
, Angular will try to iterate over an array. However, since the object is not an array, the result will be empty.

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

Are there any cross-platform inter-process communication APIs available in both C++ and Javascript?

In the process of developing an app, I am faced with the challenge of passing messages between a C++ application and a Javascript web app. While I have experience writing sockets code in both languages when required, I am now looking for a higher-level me ...

The received data object appears to be currently undefined

I am currently using MapBox to display multiple coordinates on a map, but I am encountering an issue where the longitude and latitude values in my return object are showing up as undefined. Could there be a missing configuration that is preventing the data ...

Is it feasible to indent lines in a template without affecting the content alignment?

Creating a string with newlines that will be included in an email later. if (action) { description = ` Git pull request action: ${action} Git pull request for repo: ${req.body.repository.full_name} Git pull request for repo URL: ${re ...

Filter and modify an array of objects based on a specific condition

Currently, I am fetching data from mongodb and saving it in the variable named books. The books variable is an array of objects that I am attempting to iterate through, although for this particular example, I have only included one object in the array. con ...

Angular pop-up message not displaying title or content

I have integrated the toaster directive into my AngularJS web project. Following the documentation, I have declared the container on my view as shown below. <toaster-container toaster-options="{'time-out': 3000, 'close-button':true ...

The background color of the active tab is updated upon loading the page

I have attempted to modify this code to change the background color of li tag on click. It successfully changes the background color when hovering or clicking, but unfortunately reverts back to the default color upon page refresh. I am looking for a soluti ...

Encountering an unanticipated reference error while attempting to paste the data

// Modify the layout function document.addEventListener('DOMContentLoaded', function() { function modifyLayout(productId) { // Referencing the original card and detailed card const originalCard = document.querySelector(&ap ...

Picking out specific elements from a component that is rendered multiple times in a React application

One of the challenges I face involves a component called card, which is rendered multiple times with different data. Here's how it looks: { this.state.response.reminders.map((item,i=0) =>{ return <Card key={i++} reminder={item} deleteRem={th ...

Move to the following <article>

I am currently working on developing a JavaScript function that will automatically scroll to the next article whenever the down arrow key is pressed. The challenge I am facing is that my HTML elements are all dynamic and are simply article tags without any ...

Vue.js application failing to display images fetched from the server

When I'm running my Vue.js app locally, the images are loading fine from the "src/assets" folder. However, when I deploy the app to Firebase, the images are not displaying and I get a 404 error. What could be causing this issue? I have tried using: ...

Analyze and contrast the components of two arrays using regular expressions

This is my code snippet where I am attempting to calculate the difference between elements in array1 and array2. However, the output I am receiving is not as expected. Can someone please advise on how to properly calculate the difference up to two decimal ...

Transmitting an array within the POST request payload

My goal is to send an array to my Node/MongoDB server using an AJAX POST request, along with other variables in the body. Check out the client side JS code below: function setupForm(){ $("#add").submit(function(event) { event.preventDefault() ...

The compilation of the Apache Cordova project in Visual Studio 2013 did not produce the expected release APK file

Yesterday, I successfully generated an APK release file for my VS2013 Cordova application in the bin/release folder. However, when I tried to build the APK file today, the bin/release folder is empty. I selected the release configuration, Android platfor ...

utilizing staggered animations with three.js

I am trying to animate an array of meshes in Three.js, but the properties are not being recognized. tl.staggerFrom(array, 2, {"position.y":-100}) The position.y doesn't change. When I use console.log(array[0].position.y), it gives me the initial val ...

How to make a sticky sidebar using JavaScript or jQuery

Hello everyone! I am just starting out in front end web development, so please bear with me if my question seems basic. I am trying to figure out how to create a sticky sidebar that only appears when the parent div is in view, and disappears when it' ...

Is there a way to manually trigger a re-render of all React components on a page generated using array.map?

Within my parent component (Game), I am rendering child components (Card) from an array. Additionally, there is a Menu component that triggers a callback to Game in order to change its state. When switching levels (via a button click on the Menu), I want a ...

Adjusting individual row data

Currently, I am utilizing the smart-table to showcase tabular data. Within the table, there is one editable column that can be modified within an input field. Each row contains a reset button, intended to revert any edits made in the input field back to it ...

Internet Explorer is unable to utilize the "return false" statement on a radio button

Here is a snippet of JavaScript code I use to prevent clicking on certain radio buttons. This solution works perfectly in Chrome. However, when testing in IE8, the radio button still cannot be checked and the default value disappears when another radio b ...

Tips for incorporating group by data in AngularJS

I have an array of items containing information about quantity and date. $scope.items = [ { "date": "2017-05-18T00:00:00.000Z", "quantity": 50, "id": 5 }, { "date": "2017-05-18T00:00:00.000Z", "quantity": 6, "id": 7 }, { ...

The input field for Google Places Autocomplete now includes the code "display: none"

I'm currently working on an AngularJS application and have implemented this directive to enable Google Maps API places autocomplete in the input field below. <div> <input type="text" autocomplete="off" g-places-autocomplete ...