Angular 下拉菜单和 ng-option 的最佳做法是什么

what is best practice for Angular drop down menu & ng-option

美好的一天,我在 AngularJS 上创建了一个下拉菜单。但它 return 对我来说是一个对象而不是一个项目 {"deptName":"IT","id":"1"}。我只需要 return 我的 table 的 ID。 我需要在 ng-options="item.deptName for item in department.departments" 上更改什么吗 查看示例:

JS

angular.module('firstApp', [])

.controller('EmpController', function() {
    var vm = this;
    // define a list of items
    vm.employees = [{
        name: 'Alex',
        id: '2233',
        dept: 'IT'
    }, {
        name: 'Scott',
        id: '2244',
        dept: 'IT'
    }, {
        name: 'Joe',
        id: '2255',
        dept: 'IT'
    }];
})

.controller('DeptController', function() {
    var vm = this;
    vm.departments = [{
        deptName: 'IT',
        id: '1'
    }, {
        deptName: 'Finance',
        id: '2'
    }, {
        deptName: 'Marketing',
        id: '3'
    }];
});

HTML

<div class="jumbotron">


    <h1>The Selected is </h1>
    <h2>{{main.employeeData.dept}}</h2>

    <!-- form to add computer to the list -->
    <form class="form-inline" ng-controller="DeptController as department">
        <div class="form-group">
            <label class="col-sm-2 control-label">Dept menu</label>
            <div class="col-sm-6">
                <!--<select  required="true" ng-model="main.employeeData.dept" ng-options="item.deptName  for item in department.departments">-->
                <!--<option value="">Select Category</option>-->
                <!--</select>-->

                <select required="true" ng-model="main.employeeData.dept" ng-options="item.id as item.deptName for item in department.departments">
                    <option value="">Select Category</option>
                </select>
            </div>
        </div>

    </form>
</div>

</div>

您需要像 item.id as item.deptName 一样使用 ng-optionsas 。它将在下拉值上显示 item.deptName 并将 item.id 值分配给各自selectng-model

标记

 <select required="true" ng-model="main.employeeData.dept" ng-options="item.id as item.deptName for item in department.departments">
      <option value="">Select Category</option>
 </select>

Working Plunkr这里