对于我的应用程序,我想在选择下拉列表中获取选定的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>


Controlelr:

$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

最佳答案

我已经更新了选项中的值,以保留对组和值的引用。如果组中的值是唯一的,那么我们可以使用一个值对控制器变量中的组进行映射,并从中获取任何数据。但是由于相同的值驻留在组中,因此我在opt值中保留了group的引用,并使用了该引用。

现在,在选择框中显示的文本是所需要的,并且选项的值已更新以保留组的引用。因此,两者现在都可用。

更新的Plunkr:https://plnkr.co/edit/zvNcdDe0kmMJ1R67hOkK?p=preview

控制器:

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>

08-08 02:29