The nested ng-repeat on angular seems to be malfunctioning

Utilizing an ng-repeat to display the various sections of a form, I am facing an issue with displaying the questions within each section when using a nested ng-repeat. The values are not being displayed and I'm unsure why.

Below is a sample JSON structure that I am currently working with:

{
    "section": [{
        "name": "Section 1",
        "questions": [{
            "statement": "First question",
            "id": 1
        }, 
        {
            "statement": "Question 3",
            "id": 3
        }, 
        {
            "statement": "Question 4",
            "id": 4
        }],
        "id": 1
    }, 
    {
        "name": "Section 2",
        "questions": [{
            "statement": "Second question",
            "id": 2
        }],
        "id": 2
    }]
}

Here is a snippet of my view code:

<div class="panel panel-default" ng-repeat="section in questionsTest track by $index">
    <div class="panel-heading">
        <h1>{{ section.name  }}</h1>
        <h3 class="panel-title">{{ section.name }}. {{ section.id }}</h3>
    </div>
    <div class="panel-body" ng-repeat="question in section.questions track by $index">
        {{ question.statement }} <!-- .statement -->
    </div> 
</div>

The variable 'questionsTest' holds the JSON data retrieved from the web service in the controller. This particular case uses a different JSON structure compared to other instances.

Answer №1

Give this a shot.

<div class="panel panel-default" ng-repeat="section in questionsTest">
    <div class="panel-heading">
        <h1>{{ section.name  }}</h1>
        <h3 class="panel-title">{{ section.name }}. {{ section.id }}</u3>
    </div>
    <div class="panel-body" ng-repeat="question in section.questions">
        {{ question.statement }} <!-- .statement -->
    </div> 
</div>

Answer №2

It seems that the second ng-repeat is attempting to iterate over a variable that does not exist. Since you are already inside the loop for the section, you should modify your second iteration to something like this:

<div class="panel-body" ng-repeat="question in section.questions track by $index">
    {{ question.statement }} <!-- .statement -->
</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

Is there a more concise way to write this code in JS, CSS, or HTML programming?

Seeking advice on JS / CSS / HTML programming! I may not be the best at articulating my question, so I apologize for any confusion. Let me clarify my intention behind this specific code section and explore potential solutions. The goal is to allow users ...

Is it necessary to define module.exports when using require() to import a file?

While setting up my Express server, I am using Babel to transpile my ES6 files seamlessly. In my vanilla JS server.js file, I include require('babel-core/register') and require('./app'). Within my ES6 file app.js, I handle all the usua ...

Replicating a row in a table without disrupting a button within the row

As a novice in the world of coding, I am currently embarking on a project to revamp an existing website. The task at hand involves creating a table with a built-in save/edit feature, which I have successfully implemented. However, I am facing a roadblock w ...

How come the useState hook is affecting a different state?

I have an array of data stored in the initial state as targets and another one as dataBaseTargets. My goal is to modify the value of targets without affecting the value of dataBaseTargets after a single useEffect call. However, I am facing an issue where c ...

Encountering an issue where React is unable to identify the `handleChange` prop on a DOM element

I am attempting to create a login form in React, but I keep encountering this issue. React is throwing an error stating that the handleChange prop is not recognized on a DOM element. If you want it to be shown as a custom attribute in the DOM, please use ...

Vue.js - When Property is Undefined and How to Render it in Browser

My experience with Vue has been quite puzzling. I've encountered an issue while trying to render a nested property of an object called descrizione, and although it does work, I keep receiving a warning from Vue in the console: TypeError: Cannot rea ...

Navigating through a website with just a click and automatic scrolling

My website features a left navigation bar filled with 'a' tags linked to anchor tags. As you navigate through different pages, they may not have the same song list as the homepage. I want to implement a feature where clicking an 'a' ta ...

Creating variables in Typescript

I'm puzzled by the variable declaration within an Angular component and I'd like to understand why we declare it in the following way: export class AppComponent { serverElements = []; newServerName = ''; newServerContent = &apos ...

How can you set a predetermined value for a dropdown menu after populating it with data from a Django database?

I currently have a query in my Views.py file that is used to populate a dropdown menu. The query works properly, but I need it to display a specific value that is stored in a variable based on the user's selection. For example, let's suppose we ...

How can I design a versatile button in HTML/CSS3/jQuery/Javascript that utilizes images for borders and corners for maximum scalability?

Is there a way to create a completely scalable button in HTML/CSS3/jQuery+Plugins using images for borders and corners? I have attempted a messy method, but I am confident that there are more effective solutions available. The approach I considered invol ...

A guide on emphasizing the chosen row in AngularJS

When transitioning from the 'home page' to the 'details page' in AngularJS, I want the selected row in the table to be highlighted. I attempted to achieve this with the code below, but it is not functioning as intended. Code snippet fo ...

Is there a way to generate buttons for values that are stored within an array, which is nested inside another array?

I am currently facing difficulties in creating buttons for values within an array, which are also nested inside another array. As an example: let numbers = [{"allNum": ["0", "1", "2", "3","4"]}, { ...

Save canvas as an image, with the option to incorporate additional text from a textarea element on the page into

I am currently working with a canvas element using Fabric.JS. Beneath the canvas on the screen, there is a textarea within the DOM. The download button on the page successfully downloads the canvas element as an image. However, I now need to include the t ...

Troubleshooting problems with Window.postMessage()

When attempting to fetch data from different domains, I am facing an issue. However, if I run the code on the same server, everything works perfectly fine and I am able to retrieve the message. index.html: <head> <title>Test 1</title&g ...

jQuery show/hide functionality allows you to toggle the visibility of elements

I am looking to create a toggle button that expands the menu-wrapper width and hides the sidebar when clicked. Here is the initial CSS styling: #my-wrapper { Width:500px; } #sidebar-wrapper { width:200px; } Upon clicking the button, the CSS will update ...

Pressing the icon will trigger a top-sliding dropdown mobile menu to appear

How can I ensure my mobile dropdown menu slides in from the top when the user clicks the "header-downbar-menu" icon and slides out to the top when clicked again? Currently, the button only shows the menu but I am struggling with writing the JavaScript for ...

Problem with the datepicker in mgcrea.ngStrap

Encountering an issue with the datepicker feature from the mgcrea.ngStrap library. Here is a glimpse of my layout file setup: <html lang="en-US" ng-app="ftc"> <head> <script src="/assets/2db3448a/components/angular.js/angular.min.js">&l ...

working with the variable returned by an AJAX request

I am facing an issue with my post request to handle a form submission and send an email. This is what my code looks like: $.post(url, data, function(r) { processFormSubmitResult(r); }, 'json'); function processFormSubmitResult(r) { conso ...

Limiting jQuery searches to a specific region: Tips and tricks

If I have the code snippet below: <div class="foo"> <div> some text <div class="bar"> </div> </div> </div> <div class="foo"> <div> some text <div class="bar"> some text </div> </div> </ ...

When a function is transferred from a parent component to a child component's Input() property, losing context is a common issue

I have encountered an issue while passing a function from the parent component to the child component's Input() property. The problem arises when the parent's function is called within the child component, causing the this keyword to refer to th ...