我试图在选择控件中获取所有选定的项目,例如它的值或标题。基本上,在用户从选择控件中选择了他/她想要的项目后,我想获取所选项目的数量及其标题或值。
在我的 html 页面中,我有以下代码片段:
<select ng-model="selectedValues" multiple>
<option >All Booth</option>
<option> Washroom</option>
</select>
<button ng-click="apply()" type="button">Apply</button>
在我的 Controller 中,我有以下代码片段:
$scope.apply = function () {
// Get the number of selected items and its title or value
}
有谁知道我可以实现这个目标的方法吗?谢谢。
最佳答案
请查看演示:
angular.module('app', [])
.controller('appCtrl', function ($scope) {
$scope.apply = function () {
if ($scope.selectedValues != undefined && $scope.selectedValues.length > 0) {
// Get the number of selected items and its title or value
var selectedTitles = $scope.selectedValues;//Get title or value
var noSelectedItems = $scope.selectedValues.length;//Get the number of selected items
console.log("Total items selected :" + noSelectedItems + " titles '" + selectedTitles.toString() + "'");
}
else {
console.log("No item selected");
}
}
});
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
</head>
<body ng-app="app" ng-controller="appCtrl">
<select ng-model="selectedValues" multiple>
<option>All Booth</option>
<option>Washroom</option>
</select>
<button ng-click="apply()" type="button">Apply</button>
</body>
</html>
关于javascript - angularjs - 选择的 ng-model 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41438426/