使用ng-option显示下拉值

时间:2017-03-15 00:49:29

标签: javascript html angularjs ng-options

在我的应用程序中,我一直在使用向下滚动菜单选项。取而代之的是我想使用ng-option,使得值来自js文件。我甚至需要有关角度js代码的帮助。这是我的带有选项值的html代码。需要一些帮助..

 <div class="form-group">
      <label class="control-label col-lg-2 pull-left">Quality<span class="Imp">*</span></label>
          <div class="col-lg-8">
               <select id="Quality" name="Quality" class="form-control" style="width:170px" ng-model="vm.EditRef_UI.Quality" tooltip="Quality is required"                                                                    tooltip-placement="top" required>

                  <option selected value="Satisfactory">Satisfactory</option>
                   <option value="NotSatisfactory">Not Satisfactory</option>
                </select>
           </div>

3 个答案:

答案 0 :(得分:4)

 <select ng-options="category.value as category.name for category in sourceValues"  ng-model="Quality"></select>

第一个输入category.value将是选项的值,category.name将是下拉列表中显示的值

在控制器中定义一个带有选项的数组及其要使用的值

$scope.sourceValues = [
       {value: 'Satisfactory', name: 'Satisfactory'},
       {value: 'NotSatisfactory', name: 'Not satisfactory'}

      ];

答案 1 :(得分:1)

根据documentation,您可以执行以下操作。

<select ng-options="item as item.label for item in qualities track by item.id" ng-model="Quality"></select>

答案 2 :(得分:1)

例如,如果您已将质量选项填充到范围变量“品质”中:

$scope.qualities = [{id: 1, label: 'Low Quality'}, {id: 2, label: 'Medium Quality'}, {id: 3, label: 'High Quality'}];

您可以更新您的html:

<div class="form-group">
      <label class="control-label col-lg-2 pull-left">Quality<span class="Imp">*</span></label>
          <div class="col-lg-8">
               <select id="Quality" name="Quality" class="form-control" style="width:170px" ng-options="item as item.label for item in qualities track by item.id" ng-model="vm.EditRef_UI.Quality"  tooltip="Quality is required"                                                                    tooltip-placement="top" required>
                </select>
           </div>

您需要使用如下所示的属性ng-options:

ng-options="item as item.label for item in qualities track by item.id"

您的选择仍会在范围变量vm.EditRef_UI.Quality上更新。