Exploring the ng-repeat directive with an array of objects

In the server response, I have an array of objects that I am trying to iterate over using ng-repeat in order to read each value.

While trying to use array[0].value works fine, things are not going as expected when using ng-repeat.

Despite all my efforts in debugging, I still can't grasp how ng-repeat is functioning with arrays.

Take a look at this example:

The messages array:

[
{"Id":14,"Text":"hii hello","count":750},
{"Id":10009,"Text":"test message","count":6}
]

This is what I'm using in the HTML:

<div  class="my-message" layout="row" layout-align="center center">
       {{messages}} <!-- printing the above array -->
        <div ng-repat="message in messages">
       {{ message.Id}}<!-- nothing is being printed -->

</div>
{{ messages[0].Id }} <!-- printing 14 !-->
</div>

The array is within scope and visible in the HTML since the {{ message }} array is printing correctly.

If anyone could shed some light on how ng-repeat works and where I might be going wrong, I'd greatly appreciate it.

Answer №1

Make sure to include the following code in your controller:

$scope.messages = [{"Id":1,...},...]

An error was found in your view code (ng-repat -> ng-repeat):

<div ng-repat="message in messages">
    {{ message.Id}}
</div>

The corrected version should be:

<div ng-repeat="message in messages">
    {{ message.Id}}
</div>

Everything should work properly now.

Enjoy!

Answer №2

Hopefully this explanation clarifies things for you.

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1b0bfb6a4bdb0a3ffbba291e0ffe5ffa9">[email protected]</a>" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="script.js"></script>
  </head>

  <body ng-controller="MainCtrl">
   <div  class="my-message" layout="row" layout-align="center center">
       {{messages}} <!-- displaying the array content -->
        <div ng-repeat="message in messages">
       {{ message.Id}}
         {{ message.Text }} 
         {{ message.count }} 
</div>
</div>
  </body>
<script>
  var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.messages =[
{"Id":14,"Text":"hii hello","count":750},
{"Id":10009,"Text":"test message","count":6}
];
 
});
</script>
</html>

Answer №3

I encountered a problem with a typo in my "ng-repeat" usage. Once I corrected it, my code began functioning as intended.

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

What type of JSON format is most suitable for a complex, multi-dimensional array?

Looking for advice on how to convert an excel table into a json structure. Any recommendations on the best way to do this? ...

Transmit HTML message using the "textarea" tag through email

Whenever I try to send the content of my "textarea" via email, it always ends up being sent as a blank message. How can I fix this issue? Below is my PHP code: <?php $input = json_decode(file_get_contents("php://input"), true); $ToEmail = "<a href ...

Exploring the power of ng-repeat and the flexibility of dynamic ng-model

Just a quick inquiry. I am dynamically creating a form, specifically the radio buttons section, using ng-repeat. Here is the code snippet I'm using to loop through and display the radio button options: <div ng-repeat="choice in question.choices"&g ...

Express server continuously returning 404 error for all requests originating from the index page

Experiencing a challenge with the express/gulpfile.js code. Express can serve the index.html without issue, but it fails to navigate to the lib/* directory resulting in all requests for resources at the top of my index.html returning 404 errors. Browser-s ...

Dealing with errors when loading templates in angular-ui-router

When using angular-ui-router, my configuration looks something like this: .state('page', { url: '/page', templateUrl: '/page.html' }) If the template URL returns a "401 Unauthorized" error, I would like to know if it ...

Monitor the status of an AJAX request for fetching JSON data in a Rails application

My JS code fetches JSON data from a controller method in a Rails app: $.ajax({ xhr: function() { var xhr = $.ajaxSettings.xhr(); xhr.onprogress = function(e) { if (e.lengthComputable) { console.log(e.loaded ...

Clear the Chrome Console of two warnings by resolving them in AngularJS

I'm getting ready to send out a Release Candidate of my Angular Application, but I'm having trouble resolving two warnings that keep popping up in the Chrome console. Here are the warnings: 'Attr.textContent' is deprecated. Please use ...

angular x-editable unable to clear text input when deselecting selection

UPDATE: This post has been revised to provide a more simplified example. Visit this link for the code snippet QUERY: Why does the view not update when the x-editable selection is changed to "option3" and the $scope.condition1value is set to zero (0) in $ ...

The inner nested ng-repeat section is not properly binding to the scope variable and appears to be commented out

In my code, there is a nested ng-repeat set up. The 'boards' variable in the scope is an array that contains another array called 'tasks', which also consists of arrays. In the innermost ng-repeat, I am attempting to bind to task.conten ...

The impact of array splicing on data manipulation

I have a $scope array variable that I'm using to generate tabs on the front end, but I'm struggling with implementing a remove tab function. The code for removing tabs is as follows: function removeTab(index) { $scope.tabs.splice(index, 1); ...

instructions for creating a hover effect where one div vanishes when hovering over another div

Is there a way to make the line visible when hovering over my circular div? #line { display: none } <div id='circle'> <div id= 'line'> ...

Display an array containing date objects in a dropdown menu for users to select from

I am working with an API call that returns an array of objects. Each object in the array contains a date or timestamp in ISO format. Right after my render() method, I have the following code snippet: const pickerItems = this.props.currentData.trips.map(t ...

Utilize jQuery/AJAX to extract a specific value from JSON data and transform it into a different value within the same

Hello everyone, I've been coding all day to try and solve this issue but my code doesn't seem to be working. Can anyone help me with this problem? I'm trying to convert selected JSON data into a different value. Let's take an example: ...

jQuery smoothly sliding downward from the top to the bottom

http://jsfiddle.net/Hx65Q/3/ Is there a way to make the slider open from the top instead of the left side? $("button" ).click(function() { $('.login').toggle('slide', { duration: 1000, easing: 'easeOutBounce', }); ...

Can a mutable static class variable be created in JavaScript?

I have an idea similar to this concept: class Example { static var X; static setX(value) { this.X = value; } static getX() { return this.X; } } This allows the variable to be stored within the class and easily modified ...

What could be causing the reordering of link function execution in ng-repeat?

The order in which compile and link functions are executed for nested directives is typically as follows: Markup <dir1> <div dir2=""> </div> </dir1> Execution Order 1) compile of directive 1 2) compile of directive 2 3) link ...

Locate all posts associated with the specified User ID

Using mongoose, I am able to populate the "Post Schema" with relevant information about users who create the posts. postModule.js const mongoose = require('mongoose'); const postSchema = mongoose.Schema({ title:String, description:String, date ...

Retrieving metadata using breeze is taking too long

Using breeze in my MVC 4 application to fetch data from a remote SQL Server database with about 40 entities. Surprisingly, the loading of metadata is taking longer than expected, ranging between 14s and 35s even though the size is just around 600kb. Howev ...

Storing JSON data using Vuex

As I am delving into the world of Vuex store used with vue.js, I find myself wanting to implement it in a specific scenario. 1. Is STATE referring to any data, whether static or dynamic, that is provided by the server or stored in a JSON format? TEMPLATE ...

Assemblage of Marionettes: Adding multiple elements to the DOM

I am working on a Marionette application and have come across an issue with inserting new tab items. I have a collection of tabs, but when adding a new item, I need to insert DOM elements in two different areas. The problem is that Marionette.CollectionV ...