我想使用多个选择框,这些框将由用户根据要求通过单击按钮添加字段使用相同的值添加。为了简化问题,我们假设有两个选择框:
<select name="user" id="user1" ng-model="Users" ng-options="user.fullname for User in Users">
<option value="" selected="selected" disabled="disabled">Select User</option>
</select>
<select name="user" id="user2" ng-model="Users" ng-options="user.fullname for User in Users">
<option value="" selected="selected" disabled="disabled">Select User</option>
</select>
我的数据集看起来像这样:
$scope.Users = [
{ username: joedoe, email: [email protected], id: 1},
{ username: jackblack, email: [email protected], id: 2},
{ username: mikedike, email: [email protected], id: 3}
];
我需要在每个选择框中获取所选用户的所有数据。我知道如果有一个选择框,我可以得到对象“ selectedItem”,所以我的问题是,如何在控制器中分别为每个选择框取回用户对象?
最佳答案
像这样尝试
$scope.userList=[];
$scope.add=function(){
$scope.userList.push({selected : '' });
}
的HTML
<div ng-repeat="user in userList">
<select name="user"ng-model="user.selected" ng-options="User.username for User in Users">
<option value="" selected="selected" disabled="disabled">Select User</option>
</select>
</div>
<button ng-click="add()">Add</button>
JSFIDDLE