我有一个包含ng-repeat的表单,有一些输入隐藏字段,我需要传递给服务器。 这是页面的代码:
<form ng-controller="FrmController" method="post">
<div class="orderlist">
<table class="table gas">
<thead>
<tr>
<th scope="col" ng-click="orderByField='image'; reverseSort = !reverseSort">Immagine
<span ng-show="orderByField == 'image'" class="listorder">
<span ng-show="!reverseSort"><i class="fa fa-sort-desc"></i> </span>
<span ng-show="reverseSort"><i class="fa fa-sort-asc"></i></span>
</span>
</th>
[...]
</thead>
<tbody id="gasbody" ng-repeat="product in products | filter: searchQuery | orderBy:orderByField:reverseSort" class="repeated-item">
<tr>
<input type="hidden" name="form-x-id" ng-model="form.form-x-id">
(x代表ng-repeat的索引)
[...]
<button ng-click="addtoCart();" class="def-btn" type="submit"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</button>
</form>
这是控制器的代码:
function FrmController($scope,$http,transformRequestAsFormPost){
$scope.form = {};
$scope.addtoCart = function() {
$http({
method: 'POST',
url: '',
data: $.param($scope.form),
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
});
}
}
我需要以这种格式发送数据:
form-1-id: value
form-2-id: value2
form-3-id: value3
$scope.form = {form-1-id: value,
form-2-id: value2,
form-3-id: value3, ...
... form-n-id: valuen}
其中n代表ng-repeat的长度。
... ECC
如何动态地将字段添加到绑定到ng-model的$ scope? 有可能吗?
由于
答案 0 :(得分:0)
我不准确,你想要什么? 你想要ng-repeat的索引吗?
你可以像这样使用$ index
<input name="form-{{$index}}-id" type="Hidden" ng-model="form[$index]">
或者您想动态地将某些内容推送到您的表单中吗?您可以使用:
$scope.addToForm=function(newItem){
$scope.form.push(newItem)
}
当然,您必须将表单= {}更改为表单= []