Managing various forms with dynamic controls and effectively utilizing ng-model for each AngularJS component

On my webpage, I have the ability to dynamically add forms. However, I want each form to be treated as a separate entity instead of reflecting changes across all forms. How can I achieve this separation in my application? Below is the code snippet:

HTML

<form name="referenceForm" ng-controller="ReferenceController as rfcCtrl" novalidate>
          <md-card ng-repeat="reference in rfcCtrl.references track by $index" class="job-profile-card flex" ng-click="jobCtrl.browseMatches(job)">
               <md-button ng-disabled="rfCtrl.references.length==0" ng-click="rfcCtrl.add(reference)" class="md-fab md-fab-bottom-right docs-scroll-fab job-profile-fab" aria-label="FAB">
                 <md-icon ng-bind="'add'"></md-icon>
               </md-button>
                 <div layout="column" layout-align="space-around center">
                    <div class="flex">
                    <md-input-container class="md-block" flex-gt-sm>
                        <label style="color:white">Name</label>
                        <input ng-model="rfcCtrl.ref.name" style="color:white" md-maxlength="30" ng-required="true">
                        <div style="color:white" class="hint" ng-if="showHints">Tell us what we should call you!</div>
                        <div style="color:white"  ng-if="!showHints">
                        <div style="color:white" ng-message="required">Name is required.</div>
                        </div>
                    </md-input-container>

                    <md-input-container class="md-block" flex-gt-sm>
                    <label style="color:white">Phone Number</label>
                    <input name="phone" ng-model="rfcCtrl.ref.phone" ng-required="true">
                    <div style="color:white" class="hint" ng-show="showHints">(###) ###-####</div>
                    <div style="color:white" ng-messages="referenceForm.phone.$error" ng-hide="showHints">
                    <div style="color:white" ng-message="pattern">(###) ###-#### - Please enter a valid phone number.</div>
                    </div>
                    </md-input-container>
                    </div>
                </div>
          </md-card>
                <section layout="row" layout-sm="column" layout-align="center center" layout-wrap>
                 <md-button ng-disabled="referenceForm.$invalid" ng-click="rfcCtrl.submit()" class="submit" type="submit" layout="row" layout-sm="row" layout-align="center center" layout-wrap text-align="center">Submit</md-button>
                </section>
      </form>

Javascript

function ReferenceController(referenceService, $scope, $location,$cookies) {
    var reference;
    let vm = this;
    vm.references = [reference];


     vm.ref = {
        'name': "",
        'phone': ""
    };



     vm.add=function(reference){
        vm.references.push({reference});
       }


    vm.submit = function () {

        referenceService.postReferences(vm.ref).then(function (data) {
                $location.path("/userType")
            },
            function () {
                alert("There is an error processing your request!")
            })
    };

}

If anyone has suggestions on how to accomplish this task, please share. Thank you!!

Answer №1

Ensure that you utilize the reference variable you're generating within your ng-repeat to establish ng-model. Your current approach to binding only connects to the identical object for each iteration.

<md-input-container >
    <label style="color:white">Name</label>
    <input ng-model="reference.name" style="color:white" md-maxlength="30" ng-required="true">
</md-input-container>

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

Accessing a modal in Bootstrap 4 by clicking on an anchor link will automatically open the

I am implementing a bootstrap 4 modal with anchor tags that trigger the opening of the modal. Here is an example structure: <div class="experience"> <div class="container"> <div class="row"> <div cl ...

Problem with Ext.net TabPanel

I am encountering a problem with the Ext.net TabPanel. Every time the page containing the tab panel is opened for the first time after the application has been rebuilt, it throws an error Uncaught TypeError: Object [object Object] has no method 'getCo ...

The submit button is unable to activate the form action

I am facing an issue with submitting a form to its designated action. My goal is to send contact information to a Hotmail email address, but I can't seem to make it work. I am struggling to call the Contact method in the HomeController no matter wha ...

Troubleshooting: NextJS Typescript getInitialProps returning null value

I am currently working with NextJS 'latest' and TypeScript to extract the token from the URL, but I am encountering an issue where it returns undefined. To achieve this, I am utilizing the getInitialProps method. The URL in question looks like th ...

The Button.Click event is failing to trigger

Just starting out with JQ and I've put together this fiddle: http://jsfiddle.net/SZ6mY/7/ Basically, I'm looking to display an "ALERT" message when the "C" button is clicked. Also, I'm curious about how to capture the value 7 in a variable ...

JQuery UI Autocomplete - Issue with loading data object

I've been attempting to implement autocomplete with JQuery UI, but I'm encountering difficulties when passing in a label & value object. var individuals = []; var test = new Array(); var dataObject = jQuery.parseJSON(data) ...

VARIABLE_NAME isn't functioning properly on the window

The code window.VARIABLE_NAME is not functioning properly and is causing the following error. Can you please provide assistance? Uncaught SyntaxError: Unexpected token. This is the code I have written: var window.testing ...

"Using AngularJS $http to iterate through each object in a loop and encounter a problem

I am currently developing an AngularJS app for personal training, but I have encountered a few errors along the way. The issue arises when I receive Json data from an API and try to display a marker for each object. However, I seem to have made a mistake ...

Error: Authentication providers are not yet available for login. Please check your console for any possible issues

Currently diving into the world of Angular and working on implementing Google Sign-In with OAuth. I've been running into an issue while authenticating with Google. Below is my code for handling login and logout functionality using Angular Social Login ...

Angular ng-repeat app not functioning as intended due to a lack of complexity

Seems like I've stumbled upon a rather silly issue here and it might be a duplicate question, but I'm still scratching my head over it. I have this incredibly basic Angular app along with some HTML. While debugging in Chrome, I can clearly see ...

Troubleshooting Axios Error while Sending Data in MERN Stack Application

In my development setup, I'm testing model validation specifically for the length of a name variable. The front-end is configured at http://localhost:3000/ using React + axios, while the back-end utilizes express + node. To enable communication betwe ...

Tips for running a JavaScript statement multiple times in the browser console

When I enter the following JavaScript statement in the browser console: inbreedingInvite('74011','107433',''); and press enter, it sends an invitation to user number 107433 to join group id 74011. If I want to invite another ...

Convert the jade file to an HTML file while keeping the original file name

I'm currently attempting to configure Jade in a way that allows me to save my Jade files as HTML files while retaining the same file name. For example, I would like the file views/index.jade to be saved as dist/index.html This should apply to all ad ...

How to Send C# Array as a Parameter to a JQuery Function

I'm currently working on passing a C# array to a JQuery function as a parameter. The C# code I have to call the function is: //Create an Array from filtered DataTable Column var GatepassIDs = defaultView.ToTable().AsEnumerable().Select(r => r ...

Suggestion: optimal placement for HTML table data - JavaScript or HTML?

Should I change my Python code to generate a JavaScript file instead of a webpage with a table? I am unsure of the advantages and disadvantages of this approach. Any insights or experiences to share? Are there alternative solutions that I should consider? ...

What is the best way to configure Node.js to utilize relative paths for my components?

Currently, I am in the process of developing a web component using Node.js. This specific component utilizes images that are stored within a sub-directory located in the component's main directory. Upon attempting to import the image, I encountered a ...

AngularJS: The 'myInputName' property is not defined and cannot be read

Encountering an error with AngularJS: https://i.sstatic.net/TBHem.png The issue is related to the titleInput TextBox name property: @Html.TextBox("titleInput", null, new { @placeholder = @T("Message title"), @class = "form-control", ng_model = "feed.fee ...

Leveraging require in AWS lambda operations

As part of my exploration into AWS Lambda functions, I've been trying to determine if the require statement can be used within them. This would allow me to incorporate other non-lambda functions into my code. Although zipping the node modules folder i ...

Ensure that only one button from the collection is activated

One of the features on my website includes 3 blocks with buttons. These buttons trigger a change in the displayed picture when clicked. However, it is crucial to ensure that when a new track is activated, the previous button returns to its original state. ...

Bring in a controller from another file utilizing the same module

I have a scenario with two files: File 1: test1.js angular.module("test1",[]).controller("test1Ctrl",function(){ // some code }) File 2: test2.js angular.module("test2",[]).controller("test2Ctrl",function(){ //some code }) Additionally, I have an app ...