Retrieving data from ng-model using dynamic variable names

Instead of bombarding you with lines of code, I decided to create a simple sketch to illustrate my current situation. The abstract picture gives a good overview, while the reality picture shows the actual scenario.

When I click on "Send" within bigBox_1, I want all the inputs from littleBox_1 through littleBox_n inside bigBox_1 to be passed to my function.

What would be the most effective approach to achieve this?

I attempted to dynamically name my ng-models (within littleBox_1 through littleBox_n), but I couldn't find a solution for passing all the parameters.

Here are some key points to consider:

  • The number of big boxes is unknown
  • The quantity of little boxes inside each big box varies
  • The number of little boxes in different big boxes may differ

Reality: https://i.sstatic.net/afMnx.png

Abstract: https://i.sstatic.net/hlQ3l.jpg

The model schema for the boxes: https://i.sstatic.net/ztFFk.png

Answer №1

<!DOCTYPE html>
<html ng-app="testApp" ng-controller="testCtrl">
<head>
    <title></title>
</head>
<body>

    <ul>
        <li ng-repeat="box in boxes">
            <label ng-bind="box.id"></label>
            <input type="text" ng-model="box.name" />
        </li>
    </ul>
    <button ng-click="addInput()">Add a new input box</button>
    <button ng-click="startFunction()">Start sending input values</button>


    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-rc.0/angular.js"></script>
    <script>
        angular.module("testApp", [])
        .controller("testCtrl", function ($scope) {

            var i = 0;

            $scope.boxes = []; //initialize as empty

            $scope.addInput = function () {
                $scope.boxes.push({ id: i++ });
            }

            $scope.startFunction = function () {
                console.log($scope.boxes);
            }

        });
    </script>
</body>
</html>

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

Challenges with the dropdown menu navigation bar

I am struggling with my dropdown menu and sign up/sign in buttons as they are not aligning properly. I have tried various coding methods but haven't been able to fix the issue. Can someone provide me with suggestions on how to rectify this problem? c ...

Is it possible for two Node servers to run at the same time and communicate with each other, with one acting as a mediator

After tweaking code from a rudimentary chat tutorial, I now have a JavaScript chatbot integrated into a NodeJS server. This bot immediately responds to user inputs on the client side. Here's a snippet of the relevant NodeJS server code that makes this ...

Is it possible to incorporate Vector4's into the geometry of three.js?

Exploring the functionalities of the three.js library has been a fascinating journey for me. As I delve into the intricacies, I've come to understand that the coordinates stored in a mesh's geometry are tuples consisting of (x,y,z). However, bene ...

Using Express.js to import a SQL file

I am facing challenges while attempting to import an sql file into Express JS. I have tried various codes involving the mssql and fs modules, but none seem to be working as expected. fs.readFile(__dirname +'/database.sql', function (err, sqlFile ...

Setting up dynamic routing in AngularJS for links

I am facing some confusion regarding routing in AngularJS. Normally, we can configure routes in angular.config() when the angular module is loaded. At that time, we define static information such as routePath, templateUrl, and controller. However, I am u ...

Error: Module 'config' not found by Jest

I have encountered an issue while using Jest to test my api calls file. When running a simple test, I received an error Cannot find module 'config' from 'api.service.js'. This error is related to the import statement at the top of my ap ...

Google Analytics in Next.js Missing Page Title Configuration

I recently set up Google Analytics on my Next.js website, but I'm encountering a strange issue where the analytics are not detecting my webpages and showing as (not set). Everything else seems to be functioning properly. I've double-checked that ...

Learn how to use lodash to extract the differing values between an array of values and an array of objects

I have two arrays: a1 = ['<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f480918780c5b49399959d98da979b99">[email protected]</a>','<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data- ...

My attempt to execute the JavaScript code was unsuccessful

Recently, I have been experimenting with the three.js library, a powerful javascript 3D library... Here is the code snippet I have been working on: var segments = 16, rings = 16; //var radius = 50; <-- original // Creating a new mesh with sphere geo ...

Tips on having Express automatically designate the Content-Type when utilizing res.render

When working with Node.js and using Jade as the template renderer, it is important to note that the res.render call only produces HTML output. However, it does not automatically include the corresponding Content-Type: text/html header. Is this intentiona ...

Using TypeScript to pass an array list as a parameter in the HTTP native GET method

Attempting to send an array list parameter. The codeList parameter contains an array like this: [ { "projCode": "11-1115", "cblTagNo": "571_GE001-RC1" }, { "projCode": "11-1115", "cblTagNo": "571_GE001-S" } ] Encountering the erro ...

Module import in Ionic

I'm encountering an issue with Ionic, Angular, and TypeScript, and I'm feeling a bit lost... I'm trying to call an external function from my file but I keep getting the following error: "Uncaught (in promise): TypeError: undefined is not an ...

Using Three.js to spin a mesh in the direction of a sphere

As a newcomer to Three js, I've been grappling with a challenge lately. My 3D model is currently facing a specific direction, surrounded by a sphere. Before I move the mesh, I want to animate its rotation so that it aligns with the specified sphere. ...

Error: Module not located in Custom NPM UI Library catalog

I have recently developed an NPM package to store all of my UI components that I have created over the past few years. After uploading the package onto NPM, I encountered an issue when trying to use a button in another project. The error message "Module no ...

Display Angular template when tab is in use

Just getting started with Angular and I’m working on a data dashboard that links to my own REST interface. However, the app is running incredibly slow at the moment. The structure of my app consists of different “views” that load as tab content. I ...

Limit the text input area in HTML to a fixed size, preventing any text from exceeding the specified boundary

Is there a way to create a fixed line text area that limits the user from typing beyond a certain number of lines and maximum width? My current CSS styling for this is as follows: .area-style { resize: none; width: 324px; height: 200px; m ...

Ways to retrieve the starting position of a span element and then append a value to a hidden field

Link to the Fiddle example: http://jsfiddle.net/socc95bd/1/ JQuery code snippet: var offsets = $('#ss').offset(); var left = offsets.left; $("#hfYN").val(left); alert(left); $(".switch input").on("click", function () { var offsets = $(&apo ...

What steps should I take to turn off caching specifically for directory listings?

Is there a way to generate a fresh file list every time without using cached data? I have a basic PHP script that fetches the contents of a directory and stores them in an array. The script works perfectly the first time it is used, but subsequent calls m ...

Retrieve the keys from the initial object within an array of objects

I am looking to extract a list of unique keys from an array of objects. arr =[ {id: 1, desc: "", name: "", objectives: Array(3), …}, {id: 2, desc: "", name: "", objectives: Array(3), …}, {id: 3, desc: "", name: "", objectives: Array(3), …}, ...

Connecting the SignalR client to various servers

I am currently incorporating SignalR version 2.x into my ASP.Net MVC application, which is also using the same version of SignalR in my Angular client app. The ASP.Net MVC application is hosted at http://localhost:42080, while the Angular app is hosted at ...