Javascript 如何从angularjs中的表行捕获具有相同名称的ng模型数据?
我已经创建了一个项目表,单击“添加新项目”可以动态增加该表。提交时,我希望捕获所有项目(行)信息,以便将其发送到REST服务 然而,在我的angulajs控制器中,我只能获得第一个项目(第一个表行),即使单击“添加新项目”创建了多个行 我的代码:Javascript 如何从angularjs中的表行捕获具有相同名称的ng模型数据?,javascript,angularjs,Javascript,Angularjs,我已经创建了一个项目表,单击“添加新项目”可以动态增加该表。提交时,我希望捕获所有项目(行)信息,以便将其发送到REST服务 然而,在我的angulajs控制器中,我只能获得第一个项目(第一个表行),即使单击“添加新项目”创建了多个行 我的代码: <button id="btnAddNewItem">Add Item</button> <table id="InputTable" border="0" cellpadding="0" cellspacing="0"&
<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>
实际上,我正在将所有行绑定到同一个ng模型名
我们是否需要为每个行和列维护唯一的ng模型名称?您需要使用$compiler服务或。。如果我是你,我会用这个
<button ng-click="addNewItem()">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 ng-repeat="item in items">
<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>
var myModule = angular.module('MyApp',[]);
myModule.controller('MyCtrl', function($scope, $http) {
$scope.items = [];
$scope.addNewItem = function() {
$scope.items.push({});
};
});
添加项目
项目名称
货到付款
EOT
var myModule=angular.module('MyApp',[]);
控制器('MyCtrl',函数($scope,$http){
$scope.items=[];
$scope.addNewItem=函数(){
$scope.items.push({});
};
});
请先阅读。这个问题有一个很好的答案:是的,我应该使用ng repeat来获得所有行的数组!谢谢
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
};
});
<button ng-click="addNewItem()">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 ng-repeat="item in items">
<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>
var myModule = angular.module('MyApp',[]);
myModule.controller('MyCtrl', function($scope, $http) {
$scope.items = [];
$scope.addNewItem = function() {
$scope.items.push({});
};
});