JSON object fails to iterate with ng-repeat

It must be the scorching temperature...

Having a json object that I'm eager to loop through using ng-repeat, it should be straightforward, but alas! it's just not cooperating.

Here's the HTML snippet:

<a data-ng-repeat="x in template.menuObj" href="{{ x.link }}">{{ x.name }}</a>

JSON Data:

[
    {
        "ACL":{
            "*":{
                "read":true
            }
        },
        "link":"home",
        "menu":"main",
        "name":"Home",
        "order":1,
        "objectId":"aDcb0HUXwB",
        "createdAt":"2015-08-05T15:29:05.948Z",
        "updatedAt":"2015-08-05T15:29:11.915Z"
    },
    {
        "ACL":{
            "*":{
                "read":true
            }
        },
        "link":"category/the-interesting-stuff",
        "menu":"main",
        "name":"The Interesting Stuff",
        "order":2,
        "objectId":"znXfUF0kdJ",
        "createdAt":"2015-08-05T15:33:11.332Z",
        "updatedAt":"2015-08-05T15:33:39.738Z"
    },
    {
        ...
]

Running this results in 5 empty <a> elements like this:

<a data-ng-repeat="x in template.menuObj" href="" class="ng-binding ng-scope"></a>

Seems like Angular recognizes the 5 arrays, but for some reason, the keys aren't being captured?

EDIT: The creation of the JSON object looks something like this (using parse.com):

var Menu = Parse.Object.extend('Menu');
var query = new Parse.Query(Menu);
query.ascending('order');
query.equalTo('menu', 'main');
query.find().then(function(results){
    console.log(JSON.stringify(results));
    $scope.template.menuObj = results;
}, function(error){
    // error-handling
    console.log("Error: " + error.code + " " + error.message);
});

EDIT EDIT: Controller details:

blogApp.controller('templateCtrl', function($scope, templateService) {
    $scope.template = templateService;

    var Menu = Parse.Object.extend('Menu');
    var query = new Parse.Query(Menu);
    query.ascending('order');
    query.equalTo('menu', 'main');
    query.find().then(function(results){
        console.log(JSON.stringify(results));
        $scope.template.menuObj = results;
    }, function(error){
        // error-handling
        console.log("Error: " + error.code + " " + error.message);
    });
});

templateService is a factory that connects to a parent controller. It's worth noting that prior to incorporating parse.com into this project, the ng-repeat functioned perfectly when fetching a json object via PHP/MySQL using $http.

EDIT EDIT EDIT: Check out the screen capture of console.log(results); https://i.sstatic.net/DIgqu.png

Answer №1

The issue here is that the array object is named `results` and you are assigning it to `$scope.template.menuObj` without giving it a specific name in the JSON array. You have two options:

"menuObj": [
    {
        "ACL":{
            "*":{
                "read":true
            }
        },
        "link":"home",
        "menu":"main",
        "name":"Home",
        "order":1,
        "objectId":"aDcb0HUXwB",
        "createdAt":"2015-08-05T15:29:05.948Z",
        "updatedAt":"2015-08-05T15:29:11.915Z"
    },
    // Other menu items...
]

Alternatively, you can keep the current structure but adjust the controller as follows:

blogApp.controller('templateCtrl', function($scope, templateService) {
    $scope.template = templateService;

    var Menu = Parse.Object.extend('Menu');
    var query = new Parse.Query(Menu);
    query.ascending('order');
    query.equalTo('menu', 'main');
    query.find().then(function(results){
        $scope.template = JSON.stringify(results);
    }, function(error){
        console.log("Error: " + error.code + " " + error.message);
    });
});

Update the view accordingly:

<body ng-app="blogApp">
<div ng-controller="templateCtrl" >
    <nav ng-repeat="a in template">
      <a href="{{ a.link }}">{{ a.name }}</a>
    </nav>
</div>
</body>

If you prefer to work with it as originally intended, make the following adjustments:

<body ng-app="blogApp">
<div ng-controller="templateCtrl" >
    <nav ng-repeat="a in template.menuObj">
      <a href="{{ a.get('link') }}">{{ a.get('name') }}</a>
    </nav>
</div>
</body>

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

Line of cubes - tap on a single cube and the others gracefully slide away from view

I am working on a project where I need to create a row of blocks. The goal is for the blocks to slide off the screen when one is clicked, leaving the clicked block in the first position. For instance: https://i.sstatic.net/IB5LI.jpg I attempted using jQ ...

What is the process for selecting and accessing a DOM element?

Looking to utilize jQuery selector for accessing deep within the DOM. HTML <table> ...more here <tr> <td class="foo bar clickable"> <div> <div class="number">111</div> //Trying to retrieve "111" ...

The use of WebSockets in conjunction with text encoding techniques

After reviewing the material, I came across this information: The WebSocket API allows for the use of a DOMString object, which is transmitted in UTF-8 format, or it can also accept an ArrayBuffer, ArrayBufferView, or Blob objects for binary data transf ...

How to Enhance Angular ui-router nested views with Resolve?

I have been working on creating a customized version of my Angular 1.4.12 application with nested views. The main purpose behind this customization is to accommodate sections within the app that require a header/content/footer structure, while others do no ...

Incorporate React into current Node express application by utilizing a content delivery network (CD

I'm currently in the process of developing a web application utilizing both Node and React. Instead of having separate Node and React applications, I decided to integrate React directly into my existing setup. To achieve this, I attempted importing th ...

Avoiding Re-renders in an Angular2 Countdown Component

I am facing an issue with my Angular2 master component that includes multiple child components and a standalone countdown component called "clock". The countdown component updates its label every second, causing unnecessary re-rendering of the master compo ...

Exploring SubjectBehavior within my UserService and Profile Component

Within my ShareService, I have the following code snippet: isLogin$:BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false); <==== default value is false CheckUser = this.isLogin$.asObservable(); public isLogin (bool){ ...

The `Route` component is expecting a `function` for the `component` prop, but instead it received an `object`

For a while now, I've been grappling with an issue that seems to be unique to me. The problem lies within my component and container setup for the start screen rendering at the initial route. components/DifficultySelection.jsx import React from &apo ...

Sending a collection of text inputs from a web form and saving them in MongoDB

I have been attempting to store an array of strings from my HTML form into my database (MongoDB). Here's the HTML form for creating a new class: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"& ...

What is the best way to experiment with angular-ui-tinymce within a tabset using Plunker?

I am currently experimenting with angular-ui-tinymce by incorporating it into a Plunkr. Can anyone assist me in integrating the necessary files for angular-ui-tinymce into the Plunkr so that I can showcase its functionality? Currently, I have this Plunker ...

Exploring the use of Rails and jQuery to automatically update data through the use of setTimeout and ajax calls

There's a specific page accessible in the browser at "/calendar" that directs to "calendar#index". Utilizing a javascript setTimeout function, I'm attempting to re-fetch and update data on my page using $.get ajax method. $.get("<%= calendar ...

Despite reading appsettings.json, the fields are still left empty

It seems like there might be an issue with the startup.cs file as I am unable to retrieve any values from my <IOption> config Let's take a look at our appsettings.json "Config": { "ApplicationName": "some name", "ConnectionString": "so ...

Using Redux with Next.js to implement getStaticPaths

Can someone help me understand how to implement getStaticPaths in conjunction with Redux in Next.js? I'm currently using next-redux-wrapper to manage my content, but I am encountering issues when trying to display the data. Below is a snippet of my ...

Vue.js CSS class interpolation

Is there a way to apply CSS classes dynamically in Vue.js? {{ category.icon }} --> "icon1" I am trying to achieve the following: <i :class="category.icon" class="icon icon-"></i> The desired output should be: ...

Adjust the slide count accordingly when navigating to a particular item within the Bootstrap 3 Carousel

I made adjustments to the standard Bootstrap 3 Carousel so that it can navigate to a specific slide when the URL matches #. Although this feature is working, I am facing an issue with my pager-text not updating correctly when jumping to a specific slide. T ...

Looking for a way to update template variables in copy using grunt?

I am currently utilizing load-grunt-config along with grunt-contrib-copy. My objective is to have the copy task replace certain template tags using the 'process' option. I understand that replacing template tags is feasible based on the grunt-co ...

Why isn't $.post functioning properly in any instance?

I've been experiencing issues with my $.post calls throughout my code. Despite not sending requests to different domains, everything is localhosted. My Mac OS X 10.8 automatically defined my localhost alias as ramon.local, and I'm trying to make ...

Error in table layout caused by asynchronous .get jQuery function

I am facing a challenge in populating a timetable with specific information for each cell from a database. The table is being dynamically refreshed using the following function: function refreshTable() { //Form values var park = $('#Park&apos ...

Increasing Taxes and Boosting the Overall Cost

How can we set up a system where taxes are bypassed by default unless otherwise specified when placing an order? Let's take a look at the following text box: <input class="txt1" type="text" name="subtotal" value="" id="subtotal" size="16" ta ...

Generating JSON data from a dropdown menu element

I'm working on a web page that showcases information for students in my database. One key field is their birth country, currently stored as the full name of the country. My goal is to convert these full country names into two-character strings. For ex ...