【发布时间】:2015-10-14 07:09:18
【问题描述】:
所以我正在尝试构建一个表格,该表格将输入的表单数据存储在客户端上,并将每个输入属性推送到一个组中以创建一个对象。从那里,表格是使用 ng-repeat 构建的。开始的代码如下,但没有发生任何事情。有人可以帮忙吗?
<form class="addClaim" ng-submit="submitClaim(claimInfo)">
<div class="form-group">
<label for="beneSelect">Select your benefit</label>
<select class="form-control" id="beneSelect" >
<option ng-repeat="descr in claim.claimBenes" data-ng-model="claimInfo.providerName">{{ descr.descr }}</option>
</select>
</div>
<div class="form-group">
<label for="start">Date of Service</label>
<div>
<input type="text" class="form-control" id="start" placeholder="--/--/----" data-ng-model="claimInfo.fromDate" style="width: 240px;">
<span>To</span>
<input type="text" class="form-control" id="end" placeholder="--/--/---- (optional)" data-ng-model="claimInfo.toDate" style="width: 240px;">
</div>
</div>
<div class="form-group">
<label for="providerName">Provider Name</label>
<input type="text" class="form-control" id="providerName" data-ng-model="claimInfo.provider">
</div>
<div class="form-group">
<label for="forWhom">For Whom</label>
<input type="text" class="form-control" id="forWhom" data-ng-model="claimInfo.who">
</div>
<div class="form-group" ng-show="claimInfo.benefCode == 'HCFSA'">
<label for="age">Age</label>
<input type="text" class="form-control" id="age" data-ng-model="claimInfo.who">
</div>
<div class="form-group">
<label for="claimAmount">Amount</label>
<input type="text" class="form-control" id="claimAmount" data-ng-model="claimInfo.amount">
</div>
<div class="form-group">
<label for="claimComment">Comments</label>
<textarea class="form-control" rows="5" id="claimComment" data-ng-model="claimInfo.comments"></textarea>
</div>
<div class="form-group">
<label class="control-label"></label>
<div>
<input type="button" class="btn btn-primary" ng-click="saveClaim()" value="add claim" style="float: right;">
</div>
</div>
</form>
桌子:
<div class="claimedTable" style="background-color: #ffffff; color: black;">
<table class="table">
<thead>
<tr>
<th>service date</th>
<th>provider</th>
<th>amount</th>
<th>edit</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in claimSubmit">
<td>{{ claimInfo.fromDate }}</td>
<td>{{ claimInfo.provider }}</td>
<td>{{ claimInfo.amount }}</td>
<td><a href="#"><i class="fa fa-pencil-square-o"></i></a></td>
<td><a href="#"><i class="fa fa-trash-o"></i></a></td>
</tr>
</tbody>
</table>
</div>
和控制器:
$scope.claim = [];
claimsService.getClaim().then(function (results) {
$scope.claim = results.data;
});
// submit all claim objects entered
$scope.claimsSubmit = [];
$scope.claimInfo = {
id: "",
benefitId: "",
isSecIns: "",
isNoResId: "",
expenseTypeId: "",
fromDate: "",
toDate: "",
provider: "",
who: "",
depId: "",
age: "",
amount: "",
comments: "",
isOffset: ""
};
$scope.saveClaim = function() {
$scope.claimsSubmit.push($scope.claimInfo);
//clears scope so form is empty
$scope.claimInfo = {};
}
当我在字段中输入数据并单击提交时,什么都不会离开,也不会发布到表格中。我想要作为对象而不是数组的原因是因为表格可能有多个行项目,具体取决于表单被字段输出和提交的次数。
似乎在某个简单的地方我出错了,但不知道在哪里。有什么帮助吗?
非常感谢。
【问题讨论】:
-
您是否尝试过:
{{ item.fromDate }} ? -
是的,我确实尝试过那条路线,但没有运气。
-
检查您的拼写错误...使用
ng-submit在此处plnkr.co/edit/ZMOqLs5rPx9GFwrmUDtj?p=preview进行修改 -
是的,claimSubmit 还是 claimSubmit?
-
@charlietfl - 因此,基于那个 plunker,我试图弄清楚如果浏览器意外刷新,我该如何保持这种状态。我想要做的是像你展示的那样将每个对象保存为一个对象,但是然后在我的 html 中,我将添加一个带有函数的按钮,然后将数据作为对象数组推送到 API。这可能吗?
标签: javascript html angularjs forms