javascript - Angular(ng-options)中有多个具有相同值的选择框

标签 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</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: joedoe@company.com, id: 1},
    { username: jackblack, email: jackblack@company.com, id: 2},
    { username: mikedike, email: mikedike@company.com, id: 3}
];

我需要获取每个选择框中所选用户的所有数据。我知道如果有一个选择框,我可以获取对象“selectedItem”,所以我的问题是,如何在 Controller 中单独检索每个选择框的用户对象?

最佳答案

尝试这样

$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

关于javascript - Angular(ng-options)中有多个具有相同值的选择框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34524098/

相关文章:

javascript - 三个js Canvas 如何居中?

javascript - progress[value] 样式与 react

javascript - 无效状态错误: DOM Exception 11 on XPath results

javascript - Ember onClick 事件未完成操作

jQuery:如何仅淡入永久网站元素一次?

jquery select2 与 Bloodhound.js 建议引擎

javascript - 创建自动滑动div的水平列表

javascript - 将 Jquery 添加到我的服务器 (localhost) 中的根文件夹

javascript - 如何绑定(bind)到新的Set和Map对象?

javascript - 如何在 AngularJS 中使用 console.log 表达式?