【发布时间】:2015-02-15 23:17:15
【问题描述】:
我正在使用 ui-router 在带有 AngularJS 的页面上显示两个视图。底部视图是可供选择的产品列表。页面上的顶视图是一个表单,显示供用户在选择每个项目后完成。我希望表单在最终提交时包含所选项目的值。这两个视图当前使用单独的控制器。
据我了解,AngularJS 不使用隐藏字段。将表单与所选项目相关联以便在提交时将其包含在内的最佳方法是什么?理想情况下,我想在用户单击“选择”按钮后立即将选择类型绑定到表单。
HTML:
<!--Form HTML-->
<form name="myForm">
<div class="control-group">
<label>Name</label>
<input type="text" name="name" ng-model="editProject.project.name">
</div>
<label>Special instructions for this item</label>
<textarea name="description" ng-model="editProject.project.description"></textarea>
<a href="#/" class="btn">Cancel</a>
<button ng-click="editProject.save()">Save</button>
</form>
<!--HTML for List of Items to Select-->
<tr ng-repeat="selection in selectionList.selections">
<td>{{selection.type}}</td>
<td>{{selection.cost}}</td>
<td><button ng-click="(selection.type)">Select</button>
</td>
</tr>
控制器:
//controller for form
myApp.controller('NewProjectCtrl', function($location, $timeout, Projects) {
var editProject = this;
editProject.save = function() {
Projects.$add(editProject.project).then(function(data) {
$location.path('/');
});
};
//controller for list of items to select from
myApp.controller('SelectionListCtrl', function(Selections) {
var selectionList = this;
selectionList.selections = Selections;
});
【问题讨论】:
标签: angularjs