I have created an item table which can be dynamically increased on click of 'Add New Item'. On submission I want to capture all items(rows) info for sending it to REST service.
However in my angulajs controller, I can get only the first item (first table row) even though there are multiple rows created by clicking on 'Add New Item'.
My code:
<button id="btnAddNewItem">Add Item</button>
<table id="InputTable" border="0" cellpadding="0" cellspacing="0">
<thead><tr><th>Item Name</th>
<th>COD</th>
<th>EOT</th>
</tr></thead>
<tbody><tr>
<td><input type="text" ng-model="item.name"></td>
<td><input type="text" ng-model="item.COD"></td>
<td><input type="text" ng-model="item.EOT" id=></td>
<tr>
<tbody>
</table>
Logic when click on new item:
function btnAddNewItem(){
$("#InputTable tr:last").after(
"<tr>"+
"<td><input type='text' ng-model='item.name'></td>"+
"<td><input type='text' ng-model='item.COD'></td>"+
"<td><input type='text' ng-model='item.EOT'></td>"+
"<td><img src='images/delete.png' class='btnExcluir'/></td>"+
"</tr>");
return false;
};
angularjs code is here to get items data. This shows only the first row even multiple rows exist.
var myModule = angular.module('MyApp',[]);
myModule.controller('MyCtrl', function($scope, $http) {
$scope.items[];
$scope.simulate = function(item) {
$scope.items.push(item);
alert($scope.items.length); --returns 1
};
});
I am actually binding all rows to same ng-model name.
Do we need to maintain unique ng-model names for each row column?