什么是Angular下拉菜单的最佳做法& NG选项

时间:2015-05-08 16:30:26

标签: javascript angularjs drop-down-menu ng-options

美好的一天,我在AngularJS上创建了一个下拉菜单。但它给我一个对象而不是一个项目{" deptName":" IT"," id":" 1"}。我需要只将id返回到我的表中。 我需要在department.departments"中的项目的ng-options =" item.deptName上进行更改。 看样品:

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>

1 个答案:

答案 0 :(得分:2)

您需要使用as ng-options item.id as item.deptName,例如item.deptName。它会显示item.id下拉值&amp;将ng-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>

<强>标记

username

Working Plunkr此处