在我的应用程序中,我想在选择下拉列表中获取选定的 optgroup 值。
这是代码
HTML:
<select ng-model='theModel' ng-change="display(theModel)" >
<optgroup ng-repeat='group in collection' label="{{group.Name}}">
<option ng-repeat='veh in group.Fields'>{{veh.Name}}</option>
</optgroup>
</select>
Controller :
$scope.display = function(name) {
alert(name); // i want to get the selected value and the optgroup value
}
<强> DEMO APP
所需输出: 我想显示选定的optgroup值,如果我在Collection 1下选择Field1,我需要Collection1.Field1
最佳答案
我已经更新了选项中的值,以保留组和值的引用。如果组中的值是唯一的,那么我们可以使用 Controller 变量中的值对映射进行分组,并从那里获取任何数据。但由于相同的值驻留在组中,因此我在 opt 值中保留了组的引用并使用了它。
现在选择框中显示的文本是所需的,并且选项的值已更新以保留组的引用。所以现在两者都可用。
更新了 Plunkr:https://plnkr.co/edit/zvNcdDe0kmMJ1R67hOkK?p=preview
Controller :
var app = angular.module('todoApp', []);
app.controller("dobController", ["$scope", "$http",
function($scope, $http) {
$http.get('test.json').then(function(response) {
$scope.collection = response.data.Collections;
console.log(response);
});
$scope.matches = [];
$scope.display = function(name) {
alert("controller:"+name.split("::")[0]+" and value ::"+name.split("::")[1] );
}
}
]);
HTML:
<select ng-model='theModel' ng-change="display(theModel)">
<optgroup ng-repeat='group in collection' label="{{group.Name}}">
<option ng-repeat='veh in group.Fields' value='{{group.Name}}::{{veh.Name}}'>{{veh.Name}}</option>
</optgroup>
</select>
关于javascript - 如何在 Angularjs 中获取选定的 optgroup 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39542922/