Reputation: 3317
Using ng-repeat I am creating bunch of forms with values in it. With each form there is also button to add rows to that particular form with new fields. Code is below
HTML:
<div ng-repeat="cont in contacts">
<form ng-submit="newContact()">
<input type="text" class="xdTextBox" ng-model="cont.ac"/>
<input type="text" class="xdTextBox" ng-model="cont.cat"/>
<input type="text" class="xdTextBox" ng-model="cont.loc"/>
<button type="submit">Submit</button>
<a href ng-click="addFields()">Add</a>
</form>
</div>
Javascript:
$scope.contacts = [{ ac: '', auth: '', autname: ''}];
$scope.tasks = [];
$scope.addFields = function () {
$scope.contacts.push({ ac: '', auth: '', autname: '' });
console.log($scope.contacts);
}
It is creating all the forms and the rows are added however there are two problems.:
Please let me know how i can fix it so that when add button is pressed only row in that particular form is created and when i enter text in the fields of newly pushed fields it only binds it to that particular one not to all the ones created. Thanks
Upvotes: 2
Views: 10006
Reputation: 10927
You could also use this, and just add an autoextra
attribute to you ng-repeat. New entries will be added on demand.
Upvotes: 1
Reputation: 19
Well imagine this as if you just copied and pasted your form over and over each time into the HTML. The model names don't change, and you're referring to the first element each and every time. You need to use $index to bind the correct form with its corresponding index either in your array or json.
Upvotes: 0
Reputation: 1545
I'm not sure I understand what you want to do correctly either, but it seems there are a few things not quite right here:
cont.cat
or cont.loc
at any point, even when you initialize $scope.contacts, so the ng-models won't be bound to anything.What about something like this?
<div ng-app="MyApp">
<div ng-controller="MyCtrl">
<div ng-repeat="cont in contacts">
<form ng-submit="newContact()">
<input type="text" class="xdTextBox" ng-model="cont.ac"/>
<input type="text" class="xdTextBox" ng-model="cont.cat"/>
<input type="text" class="xdTextBox" ng-model="cont.loc"/>
<input ng-if="cont.auth !== undefined" type="text" class="xdTextBox" ng-model="cont.auth"/>
<input ng-if="cont.autname !== undefined" type="text" class="xdTextBox" ng-model="cont.autname"/>
<button type="submit">Submit</button>
<a href ng-click="addFields($index)">Add</a>
</form>
</div>
</div>
</div>
And the controller's JS:
var myApp = angular.module("MyApp", []);
myApp.controller("MyCtrl", function($scope) {
// These were previously { ac: '', auth: '', autname: ''}, which is what it seems you actually want to add after clicking.
$scope.contacts = [{ ac: '', cat: '', loc: ''},{ ac: '', cat: '', loc: ''}];
console.log($scope.contacts);
$scope.tasks = [];
$scope.addFields = function ($index) {
$scope.contacts[$index] = angular.extend($scope.contacts[$index], { ac: '', auth: '', autname: '' });
console.log($scope.contacts);
console.log($index);
};
});
Here is the updated Fiddle: http://jsfiddle.net/j32SL/1/
Upvotes: 0
Reputation: 32397
Maybe I didn't understand the question right but What I think you need is a model with multiple forms of multiple contacts.
So you need nested repeaters:
<form name="{{form.name}}"
ng-repeat="form in forms">
<h2>{{form.name}}</h2>
<div ng-repeat="cont in form.contacts">
<input type="text" class="xdTextBox" ng-model="cont.ac"/>
<input type="text" class="xdTextBox" ng-model="cont.cat"/>
<input type="text" class="xdTextBox" ng-model="cont.loc"/>
</div>
<button ng-click="submit(form)">Submit</button>
<button ng-click="addFields(form)">Add</button>
<hr>
</form>
And the model looks like so:
app.controller('MainCtrl', function($scope) {
$scope.forms = [{
name: "form1",
contacts:[{ ac: '', auth: '', autname: ''}]
},{
name: "form2",
contacts:[{ ac: '', auth: '', autname: ''}]
},{
name: "form3",
contacts:[{ ac: '', auth: '', autname: ''}]
}];
$scope.addFields = function (form) {
form.contacts.push({ ac: '', auth: '', autname: '' });
};
$scope.submit = function(form){
console.log(form.contacts);
};
});
Upvotes: 7