使用angularjs在Dropdown上显示所选项目的组名?

时间:2015-07-24 03:19:50

标签: angularjs

这是我的代码



var app = angular.module('myExample', []);

function myCtrl($scope) {
  
    $scope.myOptions = [{
            "id": 106,
            "group": "Employee",
            "label": "Id"
        },{
            "id": 107,
            "group": "Employee",
            "label": "Name"
        },{
            "id": 110,
            "group": "Department",
            "label": "Id"
        }];
    $scope.getSelectedField=function(){
        //$scope.myOption=$scope.myOption;
        alert($scope.myOption);
    }; 
};

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="myCtrl">
    <select
        ng-model="myOption"
        ng-options="value.group+'.'+value.label as value.label group by value.group for value in myOptions" ng-change="getSelectedField()" >
            <option>--</option>
    </select>
    <div>
        selected value: {{myOption}}
    </div>
</div>
&#13;
&#13;
&#13;

上面的代码选择了项目显示在下拉列表中,但是我想在Dropdown上显示具有所选项目的Groupname。例如,当用户选择id现在显示&#39; Employee.id&#39;在Dropdown上。

1 个答案:

答案 0 :(得分:0)

尝试

此处value.group+'.'+value.label将显示在下拉列表中。在仅显示value.label之前。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="myCtrl">
    <select
        ng-model="myOption"
        ng-options="value.group+'.'+value.label as value.group+'.'+value.label group by value.group for value in myOptions" ng-change="getSelectedField()" >
            <option>--</option>
    </select>
    <div>
        selected value: {{myOption}}
    </div>
</div>