如何在ng模型中获取选定的选项id



我有一个选择字段,正在将所选选项值传递给客户端脚本。

<div>
<select class="custom-select" multiple id='select-user' ng-model='SelectedOptions'>
<option ng-repeat='app in data.arr track by $index' value={{app.name}} id={{app.id}}>{{app.name}}</option>
</select>
<button ng-click='selectedServers(SelectedOptions)' class='rightBtn'><i class="fa fa-arrow-right" aria-hidden="true"></i></button>
</div>
$scope.selectedServers = function (options) {
for (i = 0; i < options.length; i++) {
$scope.datas.push(options[i]);
}
console.log($scope.datas)//getting only option value
}

在这里,我还需要获得所选选项的id,并将其与选项值一起推入数组$scope.datas

如何传递带值的选定选项id?

使用ng-optionsas-for-in语法:

angular.module("myApp", [])
.controller("myCtrl", function($scope) {
$scope.data= [{
name: 'opt1',
id: 'id1'
}, {
name: 'opt2',
id: 'id2'
}, {
name: 'opt3',
id: 'id3'
}];


$scope.selectedServers = function() {      
// Here you have the selected options with both name and id
console.log($scope.selected);
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select class="custom-select" multiple id='select-user' ng-model="selected" ng-options='s as s.name for s in data'>
</select>
<button ng-click='selectedServers()' class='rightBtn'><i class="fa fa-arrow-right" aria-hidden="true"></i>Get options</button>
</div>

相关内容

  • 没有找到相关文章

最新更新