Loop over a generated form with fields using ng-repeat

I am facing an issue where I have an ng-repeat loop and I need to submit the values of input fields within it to a generated form. Using ng-model did not work for me. Is there a way to access the input names inside the form tag?

<li ng-repeat="group in tournament.groups">
    <form novalidate ng-submit="tournament.submit(group.team1, group.team2)">
        <span>
            {{ group.team1.name }}
            <input type="number" name="team1" placeholder="x">
        </span> - <span>
            <input type="number" name="team2" placeholder="x">
            {{ group.team2.name }}
        </span>

        <button type="submit">Save</button>
    </form>
</li>

Answer №1

It is important to bind the input using ng-model and ensuring that the name attribute of the form is included. Without the name attribute, your ng-submit will not be triggered.

By adding the name attribute to your form, such as name="team", Angular creates a new variable within scope containing all form-related information including validity status and field data.

HTML Markup Example:

<li ng-repeat="group in tournament.groups">
    <form name="team" novalidate ng-submit="tournament.submit(group.team1, group.team2)">
        <span>
            {{ group.team1.name }}
            <input type="number" name="team1" placeholder="x" ng-model="group.team1"/>
        </span> - <span>
            <input type="number" name="team2" placeholder="x" ng-model="group.team2"/>
            {{ group.team2.name }}
        </span>

        <button type="submit">Save</button>
    </form>
</li>

Answer №2

Implement ng-model for Two-way Data Binding:

In View:

<li ng-repeat="group in tournament.groups">
    <form novalidate ng-submit="submit()">
        <span>
            {{ group.team1.name }}
            <input type="number" name="team1" placeholder="x" ng-model="group.team1.name">
        </span> - <span>
            <input type="number" name="team2" placeholder="x" ng-model="group.team2.name">
            {{ group.team2.name }}
        </span>

        <button type="submit">Save</button>
    </form>
</li>

In Controller:

$scope.submit = function(){
   console.log($scope.group.team1.name);
   console.log($scope.group.team2.name);
};

Test out my code to retrieve the values effectively.

Answer №3

If you want to streamline your approach, I recommend consolidating multiple forms into one. You can achieve this by iterating within the form, like so:

<form name="team" novalidate ng-submit="tournament.submit(group.team1, group.team2)">
    <li ng-repeat="group in tournament.groups">

        <span>
            {{ group.team1.name }}
            <input type="number" name="team1" placeholder="x" ng-model="team1"/>
        </span> - <span>
            <input type="number" name="team2" placeholder="x" ng-model="team2"/>
            {{ group.team2.name }}
        </span>

        <button type="submit">Save</button>
   </li>
</form>

Make sure to update the ng-model for each group using '$index' (e.g., ng-model="team{{$index}}")

This adjustment should help optimize your process.

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

Selecting an option from the dropdown menu to automatically fill in a textbox within

I've run into a small hiccup with some javascripts/AJAX and could really use some guidance in the right direction. My issue involves populating the per-carton-price-field using collection_select within a form. This form is meant to generate an entry ...

Developing a unique bundle with tailored elements

I am interested in developing a custom Material UI component and making it available as a standalone package within my company's private NPM repository. Specifically, I have customized the Date Picker to create a Date Range Picker since Material UI d ...

Is there a tool that can automatically arrange and resolve TypeScript dependencies, with or without the use of _references.ts file?

Currently, I am working on understanding the new workflow for "_references.ts" and I feel like there is something missing when it comes to using multiple files without external modules in order to produce correctly ordered ".js" code. To start with, I took ...

After pressing submit, any associated links should automatically open in a separate tab

I have a homework assignment that requires creating a form with multiple elements, including a checkbox. Once this checkbox is checked, the links displayed on the resulting page should open in a new tab. The catch? I can only use HTML and CSS - no JavaScri ...

Is the asynchronous nature of setState truly reliable?

As I delve into learning React, an interesting topic that keeps popping up is the async nature of setState. It's often mentioned that if you try to console.log(state) immediately after calling setState, it will display the old value instead of the upd ...

Using JavaScript to Filter Through Numerous Values Based on Different Attributes

My goal is to efficiently filter multiple attributes with multiple values 'arr' represents a list of all products, and 'f_...' indicates the attribute such as color or type. 'namet' denotes the selected attribute by the user ...

Having trouble with CSS and javascript files not resolving after moving app to a new Windows 7 development machine

I have recently migrated my ASP.Net 3.5 web site from my old XP sp3 machine to a new Win 7 64-bit dev machine. The web application utilizes Master Pages, App_Themes with style sheets and images, as well as an image folder located off the main root. Additio ...

The JavaScript creates a select box, but the value does not get sent when the form is submitted

When a user selects a value from the first select box, a second select box is dynamically created using JavaScript. This JS triggers a PHP file to query a MYSQL database for relevant items based on the initial selection. The issue I am facing is that the ...

Creating multiple instances of an object

When using Javascript, I am trying to create an object in the following way: var testObject = { value: "this is my initial value", setup: function() { value: "foo" } }; Now, my goal is to instantiate this object and have different val ...

Creating a JavaScript library with TypeScript and Laravel Mix in Laravel

I have a Typescript function that I've developed and would like to package it as a library. To transpile the .ts files into .js files, I am using Laravel Mix and babel ts loader. However, despite finding the file, I am unable to use the functions: ...

How to transform a JSON object into an array using JavaScript

Is there a way to convert a JSON key value object into an Array that is easier to iterate through? The JSON I have looks something like this: { "01": "yes", "02": "yes", "03": "no" } However, I need the Array format below in order to easily iterate ...

Latest Information Regarding Mongodb Aggregate Operations

Struggling to toggle a boolean value within an object that is part of a subdocument in an array. Finding it difficult to update a specific object within the array. Document: "_id" : ObjectId("54afaabd88694dc019d3b628") "Invitation" : [ { "__ ...

Is it possible to retain various delimiters after dividing a String?

In the code below, the someString gets split into an array using specified delimiters in separators var separators = ['\\.', '\\(', '\\)', ':', '\\?', '!&apos ...

Effortlessly create a seamless transition in background color opacity once the base image has finished

I've set up a div with a sleek black background. Upon page load, I trigger an API request for an image, which is then displayed in a secondary div positioned behind the main one. After this, I aim to smoothly transition the overlaying div's opaci ...

Encountering a glitch when utilizing framer-motion's Animated Presence feature

I am working on a React slider where new data replaces old data whenever the user clicks on the Next or Previous button. To enhance the slider with beautiful animations, I decided to use framer motion. The animations are almost perfect, but for some reason ...

What is the best way to output information to a webpage using json_encode in PHP?

I've implemented a button on my webpage that triggers a database call using ajax, so the page can dynamically display the data without requiring a refresh. Oddly, when I click the button and inspect the source and network tab, everything seems to be f ...

Implementing dynamic option selection in Vue using JavaScript and v-model

Is there a way to manage the chosen value in a v-modeled select tag using vue.js? I created a jsFiddle demonstration, but unfortunately, it is not functioning correctly. This leads to my inquiry: vm.$set('selected', '1') // does not wo ...

Struggling with the alignment of pictures inside a container

I utilized the Instafeed.js library to fetch the three most recent images from an Instagram account. These images are loaded into a specific div and I successfully customized their styling according to my requirements. However, the current setup is quite s ...

Utilize Google Tag Manager to Safely Gather Specific Data

My current project involves capturing values from a <select> element, sending them to the Data Layer, and then forwarding them to an Analytics account via Google Tag Manager. The source code I'm trying to extract values from includes a dynamic s ...

Transferring information from JSON to HTML

Recently, I came across this handy design tool within our service that helps generate Gauge charts by simply adding a specific div class to the desired pages. <div class="gauge" id="g0" data-settings=' { "value": ...