The ng-repeat in the inner loop is excluding the initial element of my array and subsequently placing them in the final HTML tag

I am facing a challenge with converting a JSON array into HTML using angular's ng-repeat.

The JSON structure I'm working with looks like:

data:{
thing_one:[{
       id:1,
       fields:[{ .... }]
    },
    {
       id:2,
       fields:[{ .... }]
    }],
separate_thing:[{
       id:1,
       fields:[{ .... }]
    },
    {
       id:2,
       fields:[{ .... }]
    }]
}

In the controller for that page, I have:

The following code snippet:

MockFields.get(function(data){
    $scope.thing_one = data.thing_one;
    $scope.separate_thing = data.separate_thing;
}

In my HTML template, it is structured as follows:

<div class="details">
    <div class="my_header">
        <h2>Thing 1</h2>
    </div>
    <div class="thing_one">
        <dynamic-form fields="thing_1[0].fields"><dynamic-form>
    </div>
</div>
<div class="details">
    <div class="my_header">
        <h2>Separate Thing</h2>
    </div>
    <div class="separate_thing" ng-repeat="thing in separate_thing>
        <dynamic-form fields="thing.fields"><dynamic-form>
    </div>
</div>

The issue arises when inspecting a specific element in the DOM:

<div class="details">
    <div class="my_header">
        <h2>Separate Thing</h2>
    </div>
    <div class="separate_thing" ng-repeat="thing in separate_thing></div>
    <div class="separate_thing" ng-repeat="thing in separate_thing>
        <div>
             <label>Foo from separate thing 1</label>
             <input type="text">
             .....
       </div>
       <div>
             <label>Foo from separate thing 2</label>
             <input type="text">
             .....
        </div>
    </div>
</div>

It seems that all the sub-objects (fields) are being pushed to the last <dynamic-form>, leaving the first one empty.

This behavior concerns me - could this possibly be an optimization by Angular or have I made an error somewhere? I have not encountered any errors in my code so far.

Any insights on why the sub-objects are consolidating into the last element would be greatly appreciated.

Answer №1

It appears that there was a potential glitch in the system.

I encountered an issue with Angular directives failing to compile due to having multiple elements without a containing element, which was present in my previous setup (using ng-repeat).

To resolve this, I made a modification by wrapping the div with ng-repeat inside an outer div within my directive.

As a result, the structure now looks like this:

<div>
    <div ng-repeat='field in fields track by field.order'>
        <label>{{field.display}}</label>
        <span ng-switch on='field.fieldtype'>
            <span ng-switch-when="text">
                <input type="text"/>
            </span>
            <span ng-switch-when="....">
                <input type="...."/>
            </span>
             .
             .
             .
             .
        </span>
    </div>
</div>

After implementing this change, everything seems to be functioning properly.

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

Executing multiple HTTP requests simultaneously in groups using an asynchronous for loop for each individual request

I'm currently working on running multiple requests simultaneously in batches to an API using an array of keywords. Read Denis Fatkhudinov's article for more insights. The issue I'm facing involves rerunning the request for each keyword with ...

Issue with v-model not connecting to app.js in Laravel and Vue.js framework

Snippet from app.js const app = new Vue({ el: '#app', router, data:{ banana:'' } }); Code found in master.blade.php <div class="wrapper" id="app"> <router-view></router-view> //using Vue ...

Is there an image overlapping another image?

Currently, I am working on developing a website for a Food Cart project in my class. One of the key features I want to incorporate is the ability for users to click on an image of a plate and have a different image representing the food appear on top of it ...

Get ready for some Angular validation appearing on the screen

Using AngularJS, I have set up routes and validations on the HTML page. Here is a snippet of my HTML code: <form ng-controller="validateCtrl" name="loginForm" novalidate> <p>UserName:<br> <input type="text" name=" ...

What could be causing the failure to retrieve the salt and hash values from the database in NodeJS?

My current issue involves the retrieval of hash and salt values from the database. Although these values are being stored during sign up, they are not being retrieved when needed by the application. Below, you will find snapshots of the database, console s ...

Prevent users from selecting elements on the mobile site

Hey there! I'm currently working on preventing users from selecting items on my mobile site. While I've been successful in doing so on a regular website using the CSS class below .unselectable { -moz-user-select: -moz-none; -khtml-user-s ...

How to store and retrieve images using the Google Drive API in a Node.js application

I am new to working with Node.js. I have been exploring how to route an incoming image from the Google Drive API without having to download it first, as that process takes too long. My goal is to send the image directly to the client once I receive it. I ...

Updating token (JWT) using interceptor in Angular 6

At first, I had a function that checked for the existence of a token and if it wasn't present, redirected the user to the login page. Now, I need to incorporate the logic of token refreshing when it expires using a refresh token. However, I'm enc ...

In Vue, emitting does not trigger the parent component to update its properties

The emit is functioning properly, as I can observe in the vue developer tool. However, the property in the parent element is not updating. Child Component: <template> <div> <ul> <li v-for="(option, index) in opt ...

Delineating the execution of a PHP function through a button click using HTML/Javascript

I have a need to trigger a specific PHP function by simply clicking on a button. Currently, I am working with the following code snippet: <table class='standardtable'> <tr> <th>Event</th> <th>Group</th> ...

Improving the performance of a function that generates all possible combinations of elements within an array

After coming across this function online, I decided to optimize it. For instance, if we have an input of [1, 2, 3], the corresponding output would be [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] Below is the code snippet: const combinations = arr = ...

Unable to revert input to its previous state after setting the value attribute in ReactJS

It may seem strange, but I set the value attribute of the input tag to a random state. However, I am unable to type anything into the input field. The input field is meant to be cleared after clicking, but unfortunately, nothing happens. (I apologize if ...

Utilizing jQuery to send multiple values via an ajax request

I am looking to modify this script to send multiple values using AJAX instead of just a single value. files.php $(".search_button").click(function() { var search_word = $("#search_box").val(); var dataString = 'search_word='+ search_word ...

Leverage AngularJS to effectively parse JSON data using scope variables

I am trying to JSON parsing in AngularJS using $stateParams in the controller below: rerunApp.controller('rerunCategoryListCtrl', function($scope, $http, $stateParams) { var stpNameCat = $stateParams.nameCat; $http.get(JSON UR ...

Serialize and deserialize JSON data with a Dictionary using the .NET DataContractJsonSerializer serializer

First and foremost, I am exclusively focused on using DataContractJsonSerializer and not interested in any other parsers like JSON.NET. My task involves working with data structures that are exchanged with a REST API and they have the following format: { ...

Consistently receiving the identical result even when the checkbox remains unselected

Displaying a Checkbox Input <input id="idCheckbox" name="check" type="checkbox" value="AllValue" style="width: auto; height: auto; font-weight: bolder;" data-bind="checked: idCheckbox" /> The checkbox input will always have the value "AllValue ...

Incorporate a JavaScript script into an Angular 9 application

I have been experiencing issues trying to add a script.js file to angular.json and use it in one component. Adding a script tag directly to my HTML file is not the ideal solution. Can someone suggest an alternative approach or point out what I may be missi ...

Is there a way to attach a hidden input to the file input once the jquery simpleUpload function is successful?

Attempting to add a hidden form field after the file input used for uploading a file through the simpleUpload call. Here is the HTML (loaded dynamically): <div class="col-md-6"> <div class="form-group"> ...

How do I access the previous and current values in a v-for loop in Vue.js in order to compare them?

I am working on a structural component that involves looping through a list and performing certain actions based on the items: .... <template v-for="(item, INDEX) in someList"> <template v-if="thisIsArrayList(item)"> ...

Is there a way to modify the state following a sorting operation?

How can I properly update the state after sorting by salary? state = { workers: [ {name: 'Bob', surname: 'Meljanski', salary: 5140}, {name: 'Michel', surname: 'Hensson', salary: 5420}, {n ...