0

Please click on the demo below

www.jsfiddle.net/rnnb32rm/1370

My problem is: "Add input" is working fine. But whenever i invoke "Add Fields", the subsequent field will be sync with the first one. I want the subsequent to be filled with only one input. Stuck for hours already. Please guide!

Picture to illustrate: enter image description here

enter image description here

2 Answers 2

1

May be help you.

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

app.controller("MyCtrl" , function($scope){
  
   $scope.data ={
       items:[{ name:"",family:"",age:""}]
   };
  
  $scope.addRow = function(index){
    var item = { name:"",family:"",age:""};
       if($scope.data.items.length <= index+1){
            $scope.data.items.splice(index+1,0,item);
        }
    };
  
  $scope.deleteRow = function($event,item){
  var index = $scope.data.items.indexOf(item);
    if($event.which == 1)
       $scope.data.items.splice(index,1);
    }
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   <table>
     <tr ng-repeat="name in data.items track by $index">
        <td> <input type="text" ng-model="data.items[$index].name"></td>
         <td> <input type="text" ng-model="data.items[$index].family"></td>
          <td> <input type="text" ng-model="data.items[$index].age"></td>
        <td> <input type="button" ng-click="addRow($index)" value="Add" ng-show="$last"></td>
        <td> <input type="button" ng-click="deleteRow($event,name)" value="Delete" ng-show="$index != 0"></td>
     </tr>
   </table> 
    <span>{{data|json}}</span>
</div>

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

Comments

1

I answered this the first time you posted this question, but you deleted it.

You only have one choices array, and you are using it over and over:

$scope.addNewChoice = function() {
    // ...
    // Same array each time
    $scope.choices.push({'id':'choice'+newItemNo});
};

<fieldset  data-ng-repeat="choice in choices2">
    <div  data-ng-repeat="choice in choices "> <!-- Same array each time -->

You probably want one array for each entry in the choices2 array.

Also, both of your ng-repeat elements use the same variable name (choice) which is confusing.

Comments

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.