Creating interactive ng-models for form controls using AngularJS

I am working on a project where I have an Array in my controller. This Array is being used to dynamically generate input fields on the page.

Here is a snippet of my AngularJs code:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.names = ['morpheus', 'neo', 'trinity'];
});

After setting up the controller, I proceed to generate the input fields on the page using the following code:

<form name="myForm1" ng-controller="MainCtrl">
     <div ng-repeat="gg in names">
       <input type="text" ng-model="control[index]"/>
     </div>
     <input type="submit" value="submit"/>
</form>

Currently, each textbox is being generated with ng-model as control[index]. However, I would like to change this so that the ng-model for each textbox follows this pattern:

control[0]
control[1]
control[2]

If you'd like to see the code in action, check out this Plunker.

Answer №1

It is necessary to utilize the following code:

<input type="text" ng-model="control[$index]"/>

Furthermore, make sure to define the scope variable control if it does not already exist:

$scope.control = {};

For an updated version, refer to this Plunker

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 reason behind the render function not being triggered when setState is used in the componentWillUpdate() and componentWillMount() methods?

Is it possible to call setState() within any of the component lifecycle methods? Have you ever wondered why we avoid calling it inside componentWillUpdate() and componentWillMount()? What is the reason behind these methods not causing the render function ...

Having trouble establishing a connection to SQL Server with tedious in Node.js

Trying to connect to the SQL Server "SQL.SCHOOL.EDU\STUDENTSQLSERVER,4500" from my school has been a real challenge for me using tedious. I am currently working on setting up a connection between my express back end and react front end. For now, I am ...

How can I update the color of a list item when it is clicked within a foreach loop using knockout js?

Currently, I am encountering an issue with changing the color when a user clicks on a list item using the latest version of knockout js. My goal is to change the color of a list item when it is clicked and maintain that color until another item is clicked, ...

"Utilizing AngularStrap for dynamic popovers attached to anchors

Is it feasible to trigger a popover using AngularStrap Popover from an anchor or span element? This scenario works: <button content="hello" trigger="focus" bs-popover>clickme</button> However, the following attempts do not work: <a href= ...

Using the Match() function with an array of objects

I am working with an object array containing multiple variables. let Novels = []; class Novel { constructor(isbn, title, author, edition, publication, year) { this.isbn = isbn; this.title = title; this.author = author; this.publicat ...

Verifying that utilizing a factory is the most effective method for developing a versatile click count listener

In my quest to enhance the functionality of click events, I have devised a click count factory. This factory creates a clickCountObj to keep track of the number of clicks and implements a new function for capturing click events on a specified element and r ...

Safari users may encounter issues with Web Audio playback in iOS 15 when the screen is locked for an extended period of time

After updating my iPod Touch to iOS 15 (15.0.1), an issue arose. Initially, the example below works perfectly, allowing me to play the sound multiple times without any problems. However, if I lock the screen on my iPod Touch and return a few minutes later ...

Having trouble with the HTML5 canvas for loop not rendering the initial object in the array?

Essentially, I'm attempting to iterate through each letter in a text string (specifically the text "marius"). However, there's an issue where the first letter is not being displayed. When the text is "marius", only "arius" is drawn. I've exh ...

Anguar Module Naming Conventions: Best Practices

While studying AngularJS, I came across a helpful article on the topic from this site: AngularJS The article referred to AngularJS modules using syntax like my.new.module var module = angular.module( "my.new.module", [] ); I am curious about the signifi ...

An issue has been encountered in NodeJS with a route that begins with a percent sign causing an error

I have a NodeJS server set up with the following backend configuration: app.use(express.static(__dirname)) app.get('/', function(req, res){ res.send('main page here') }); app.get('/*', function(req, res){ res.send(&apos ...

What steps can be taken to diagnose the cause of a failed Jquery AJAX request?

I am attempting to utilize the Yahoo Finance API to retrieve data in CSV format through Javascript. However, my current implementation shown below is not successful. $.ajax({ type: "GET", url: "http://finance.yahoo.com/d/quotes.csv?s=RHT+MSFT&f=sb2b3j ...

Navigate to the top of the page prior to updating the Link route in NextJS

Whenever I click on a link to navigate to a new page, the page loads and immediately scrolls to the top. I want to change this behavior so that the scroll resets to the top before the new page is rendered. You can observe this issue on . If you scroll do ...

The module '../xcode' could not be located. This issue is occurring within React Native and Expo CLI, where the required stack cannot

Trying my hand at creating my first project using React Native in iOS with expo.io, I encountered an error when running the command "expo start": https://ibb.co/f2xsmpN https://i.sstatic.net/Uyxkk.png Despite attempts to reinstall and update Xcode, usin ...

Running the command "npm start" is resulting in an error message stating: "ENOENT: no such file or directory ... package.json"

After successfully creating a React app using NPX, I encountered an issue when trying to run npm start. The error message that appeared is as follows: $ npm start npm ERR! code ENOENT npm ERR! syscall open npm ERR! path C:\Users\Administrateur.TE ...

Having trouble running a form due to the inclusion of JavaScript within PHP code

My PHP code includes a form that connects to a database, but when I add JavaScript to the same file, the form does not execute properly. (I have omitted the insert code here.) echo '<form action="$_SERVER["REQUEST_URI"];" method="POST">'; ...

exploring the capabilities of sockets in PHP, reminiscent of the functionality found in Node.js

I recently downloaded and tried out a basic chat app with Node.js: https://github.com/socketio/chat-example The app is functioning properly. The server-side code is quite straightforward: var app = require('express')(); var http = require(&ap ...

Utilizing Angular Directives and Controllers for Improved Efficiency

I have developed a custom dropdown option selector which is functioning well. It includes functions to fetch data from a specified URL in order to populate a list. However, the issue arises when I attempt to reuse this component in a different section of ...

Employing an object from a distinct module

After creating a function to parse objects and provide getters, I encountered an issue. I need to access this object from a different module without re-parsing it each time. Is there a way to achieve this without using a global variable? var ymlParser = r ...

Arranging serialized information from a tabular format and retrieving specific data

: I'm currently attempting to utilize an AJAX POST request to send data from a dynamic webpage that includes information gathered from a table form, a date selection box, and comment text input. The webpage is generated as a PHP file on a GET request ...

How can you retrieve a value from a JavaScript closure function?

I'm struggling with getting a value from a closure function in Javascript. In my initial attempt, I placed the return statement inside the inner function, but it was not effective. Then, I tried moving the return statement to the outer function, howev ...