Javascript 在具有相同值的角度(ng选项)中具有多个选择框
我想使用多个选择框,用户将根据请求通过单击按钮添加字段,使用相同的值来添加这些选择框。为了简化问题,我们假设有两个选择框:Javascript 在具有相同值的角度(ng选项)中具有多个选择框,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我想使用多个选择框,用户将根据请求通过单击按钮添加字段,使用相同的值来添加这些选择框。为了简化问题,我们假设有两个选择框: <select name="user" id="user1" ng-model="Users" ng-options="user.fullname for User in Users"> <option value="" selected="selected" disabled="disabled">Select User</optio
<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>
我需要在每个选择框中获取所选用户的所有数据。我知道,如果有一个选择框,我可以获取对象“selectedItem”,因此我的问题是,如何在控制器中分别检索每个选择框的用户对象?ng模型将在选择框中保存所选项目的值。而
ng选项
将填充选项列表
您需要在选择的元素中指定不同的ng模型
例如,用户1和用户2
<select name="user" id="user1" ng-model="user1" 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="user2" ng-options="user.fullname for User in Users">
<option value="" selected="selected" disabled="disabled">Select User</option>
</select>
<button ng-click="displaySelected()">Display selected</button>
$scope.user1 = null;
$scope.user2 = null;
$scope.Users = [
{ username: joedoe, email: joedoe@company.com, id: 1},
{ username: jackblack, email: jackblack@company.com, id: 2},
{ username: mikedike, email: mikedike@company.com, id: 3}
];
$scope.displaySelected = function() {
console.log('items selected', $scope.user1, $scope.user2);
}
选择用户
选择用户
显示所选内容
$scope.user1=null;
$scope.user2=null;
$scope.Users=[
{用户名:joedoe,电子邮件:joedoe@company.com,id:1},
{用户名:jackblack,电子邮件:jackblack@company.com,id:2},
{用户名:mikedike,电子邮件:mikedike@company.com,id:3}
];
$scope.displaySelected=函数(){
log('items selected',$scope.user1,$scope.user2);
}
像这样试试
$scope.userList=[];
$scope.add=function(){
$scope.userList.push({selected : '' });
}
HTML
选择用户
添加
这只是另一种选择。。。因此,您可以动态设置所有内容 将选定选项的容器定义为作用域中的对象
$scope.selectedUserContainer = {};
在html中
<select name="user" id="user1" ng-model="selectedUserContainer['user1']" ng-options="user.fullname for User in Users">
<option value="" selected="selected" disabled="disabled">Select User</option>
</select>
酷!我刚刚在JSFIDLE中做了一些更改:还有一件事:是否可以让一组字段直接用于输入数据,而不是用按钮显示为空?
$scope.selectedUserContainer = {};
<select name="user" id="user1" ng-model="selectedUserContainer['user1']" ng-options="user.fullname for User in Users">
<option value="" selected="selected" disabled="disabled">Select User</option>
</select>
{"user1": whatever selected, "user2": another one}