1

I am currently trying to figure out how to keep reference to individual dynamically added input fields. I have a modal popup as follows:

<div ng-controller="SamplesQueryController">
            <button ng-click="toggleModal()" class="btn-splash-small">Add Sample</button>
            <modal title="Create New Sample" visible="showModal">
            <form role="form">
                Sample Name:<br> 
                <input type="text" ng-model="newSampleName.sampleName"> 
                <br> Attribute 1 Name:<br>
                <input type="text" ng-model="newAtt1Name.att1Name">
                <br> Attribute 1 Value: <br> 
                <input type="text" ng-model="newAtt1Value.att1Value"> <br>
                <button id="submitSample" ng-click="createSample();toggleModal()">Submit
                    Sample</button>
                <button id="addAttribute">Add Attribute</button>
                <button ng-click="toggleModal()">Close</button>
            </form>
            </modal>
        </div>

which currently has an input field for att1Name and att1Value, I have an addAttribute button which should add 2 new input fields (for att2Name and att2Value). I can dynamically create the inputs using a method such as:

<input type="text" ng-repeat="myinput in myinputs" ng-model="myinput">
</input>

but how can I keep reference to each of the typed in values in the input fields and how can I create 2 fields for each element in myinputs

Preferably, I would be storing these values in some sort of structure like attributes.att1.name, attributes.att1.value, attributes.att8.name, etc

3
  • see "Binding to form and control state" here: docs.angularjs.org/guide/forms Commented Feb 4, 2016 at 21:54
  • still pretty lost after rereading the doc Commented Feb 4, 2016 at 22:06
  • I think you're getting stuck because your domain model is wrong. Think of a Sample, with a Name, and an array of Attributes. Each Attribute has a Name, and a Value. You can then map your modal form to a Sample, and in the form, have an ng-repeat="attribute in sample.Attributes". Commented Feb 4, 2016 at 22:27

2 Answers 2

3

You'll want to have your model as an Array that contains Objects, which has the property 'name' and 'value' initiated. Then it's pretty easy to create a ng-repeat div, that contains 2 text input fields:

<div ng-repeat="input in inputs">
  <input type="text" ng-model="input.l">
  <input type="text" ng-model="input.v">
</div>

Then you can subsequently add the fields by pushing newly initiated object to the $scope.inputs.

function add() {
  var obj = {attr1Name: '', attr1Value: ''};
  $scope.inputs.push(obj);
}

Working fiddle here: https://jsfiddle.net/ccvLhmps/

Sign up to request clarification or add additional context in comments.

1 Comment

I have implemented a similar solution, but I want to have different no-model for every input field. For example the first pair would have input.l1 and input.v1, while the second pair input.l2 and input.v2 and so on. How can that be achieved?
-2
<!DOCTYPE html>
<html>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
   <body>
      <h2>Cost Calculator</h2>
      <p></p>
      <p></p>
      <p></p>
      <div ng-app>
         <input  name="amount"  ng-model="a" placeholder="Amount"  />
         <input  name="adjustment"  ng-model="b" placeholder="Adjustment"  />                           
         <input  name="advance"   ng-model="c" placeholder="Total"  value='{{ a-b}}' />                             
         <input  name="balance"  placeholder="Total" readonly required value='{{ a-b-c}}' />
      </div>
   </body>
</html>

1 Comment

Please explain your answer in context of OP's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.