Developing dynamic forms within arrays using AngularJS

Hey there! I've got a scenario where I have an array of people in my dynamic application. Each person, such as James, Bob, and Walter, has their own set of data that needs to be filled out using simple directives.

  $scope.users = [
    {
      name: 'James',
      data: []
    },
    {
      name: 'Bob',
      data: []
    },
    {
      name: 'Walter',
      data: []
    }
  ];

These forms come in the form of $scope.basicDataFormData and $scope.styleRequestFormData, which are then attached at the main level like this:

  $scope.data = {
    basic_data: $scope.basicDataFormData,
    style_request: $scope.styleRequestFormData
  }

The user's page is determined by $scope.menu.currentIndex, resetting to

0</code or increasing with each click on "Next user".</p>

<p>However, the problem arises when trying to assign the form data to a specific user. It seems that the forms get copied over to all users instead of being allocated based on the current <code>$scope.data
.

Here's the working example: https://plnkr.co/edit/zGVPViVTkepGB1ZO3PbU?p=preview

As shown in the example, the form data is not assigned correctly to individual users. Instead, all users end up with the same data, even though the index should only correspond to one active user at a time.

Can anyone offer some insight into how to resolve this issue?

The intended functionality is for each form to be saved to the current user's data object based on the menu.currentIndex. This way, each user's form data remains persistent within their own view. James should have two objects in his data while Bob and Walter should each have just one.

Answer №1

In the initialize function, you're currently assigning one object to another in this manner:

function initialize() {
    $scope.data.basic_data = $scope.basicDataFormData;
    $scope.users[$scope.menu.currentIndex].data = $scope.data;
}

This approach is incorrect as it simply refers them to the same memory location. Instead, you should aim to duplicate the object. Refer to https://docs.angularjs.org/api/ng/function/angular.copy.

The corrected function would look like:

function initialize() {
    $scope.data.basic_data = angular.copy($scope.basicDataFormData);
    $scope.users[$scope.menu.currentIndex].data = angular.copy($scope.data);
}

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 is the best method to modify the accurate phone number within my script?

I need help with a text format script. link HTML CODE: <label for="primary_phone">Primary Phone Number<span class="star">*</span></label> <br> <input type="text" name="primary_phone" id="primary_phone" class="_phone requ ...

Express file response problems affecting React front-end communication

I am currently developing a basic questionnaire on a React full stack website that uses React, Express, and SQL. Right now, my main goal is to return an error message from React and have it displayed on the front end. This is the code for my express endp ...

The array does not store the ObjectId

I'm trying to implement the favoriting feature following a tutorial, but I'm encountering issues with making it work. Any assistance would be greatly appreciated. Thank you! UserSchema: var UserSchema = new mongoose.Schema({ username: {type ...

With the power of jQuery, easily target and retrieve all label elements within a specified

Currently, I'm working on developing a function that should be executed whenever any of the labels for a particular group of radio buttons are clicked. So, I need a way to reference all the labels in this radio button group. In my search for a soluti ...

Bidirectional Communication between ExpressJS and Mongoose Models

Let's consider a scenario where we have a User model and a Book model in our express app, both referencing each other. How can mongoose middleware be utilized to ensure that both models stay synchronized when saving either one? It would be beneficial ...

The function Expo.Fingerprint.isEnrolledAsync will provide an output that includes information about fingerprint

Attempting to incorporate fingerprint authentication into my react native app. Utilized Expo SDK for this purpose. Although the Expo.Fingerprint.authenticateAsync() method claims to return a boolean, it actually returns an object when examined closely. E ...

What is the best method for retrieving a child control from a TR element?

I am facing an issue with a hidden field inside each Tr in my template: <ItemTemplate> <tr style="" class="ui-selectee trClass ui-widget-content"> <td style="width: 100px"> <asp:HiddenField ID="idField" runat=" ...

Is it feasible to utilize express.static twice in Express.js 4.x?

I am seeking to create a unique 404 page that includes HTML, CSS, images, and icons. Upon reviewing my project structure, I have observed that my 404 folder functions correctly when replacing the initial public static homepage. However, I suspect that I ma ...

When an onClick event is triggered in jQuery, generate a certain number of div blocks based on the available list items, such as image source and heading text

Is it possible to generate input fields dynamically based on a dynamic list with checkboxes, labels, text, images, etc.? I currently have a working solution for checkboxes and labels using the code snippet below: let $checkboxContent = $('.checkboxes ...

Error: The JSON input unexpectedly ended, however the PHP file itself is error-free

When trying to display data retrieved using PHP through JSON/Ajax, I encountered an error message: [object Object] | parsererror | SyntaxError: Unexpected end of JSON input The PHP script is functional (I can view the JSON output by directly accessing th ...

Removing a row from the database using AngulaJS Ui-Grid

Greetings! I am currently working on a small application utilizing AngularJS and Ui-grid. One of the pages displays all rows from a specific table within the Ui-grid. The grid includes a delete button in the last column for removing individual rows. Below ...

Difficulty encountered when attempting to implement custom filtering based on condition in HTML using Angular

I'm still new to angular, so please bear with me if this question seems trivial. I have a collection of integers in my controller and I need to filter it to only show items that meet a certain condition. Initially, I attempted the following: <div ...

Steps to create a custom function that can manage numerous onclick actions to toggle the visibility of a specific field

I'm relatively new to coding and JavaScript. I'm working on a basic webpage that involves showing and hiding parts of sentences for language learning purposes. Is there a way to create a single function that can show and hide the sentence when a ...

What is the proper way to provide parameters in a GET request using Axios?

Recently, I have been attempting to include the api_key in the get request parameter using axios Below is the snippet of my code: const instance = axios.create({ baseURL: "https://api.themoviedb.org/3" }); export function crudify(path) { function get ...

Combining Option Value and Name Selection Using React Hooks

Currently, I am facing a situation where I need to retrieve two different values (item.id and item.name) to set the State when selecting an item from my dropdown menu. Right now, I am only able to do this for one value (item.id). Is it possible to achieve ...

Encountering difficulties displaying a webpage in Express.js following a successful DELETE request

While working on a URL shortening project using Node.js, MongoDB, and ejs, I encountered an issue when trying to render a page after successfully handling a DELETE request. Although the 'deletion' page is loaded in the response preview in the bro ...

How to efficiently switch between classes in Ember Octane using Handlebars?

What is the best way to toggle between displaying a class on and off using Ember.js Octane? Should I use an @action or @tracked in this case? <img src="flower.jpg" alt="flower" class="display-on"> or <img src="flower.jpg" alt="flower" class=" ...

"What is the best way to have a data toggle in Vue that waits for the success or failure of

How can I ensure that the modal is triggered only after a successful axios response, without altering any existing styles? The current code triggers the modal before waiting for the axios response, despite adding v-if. Current code: <template> <d ...

What could be causing this code to keep looping?

This is my submission for a class project. The task given was: "Create a function that will kickstart the program and name it main(). From the main() function, invoke a function named getValue(). The getValue() function will prompt the user to input a num ...

The time selector does not appear in the v-date-picker widget

I need help figuring out how to select a time range using v-date-picker on vCalender within my Vuetify project. Despite setting the mode of v-date-picker to dateTime, I am unable to find an option to choose the time along with the date. Is there something ...