使用ng-option时设置选定的标签

时间:2017-03-15 07:40:04

标签: html angularjs select ng-options selected

我有这个代码,通过ng-option将值加载到下拉列表中。但是当我使用ng-option时,我无法设置默认值。请在这里找到我的代码并帮助我。

               <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="Q.value as Q.name for Q in vm.quality" ng-model="vm.Edit.Quality" tooltip="Quality is required"
                  tooltip-placement="top" required>

                             </select>
                        </div>
                  </div>

角度代码在这里:

 vm.quality = [{ value: 'Satisfactory', name: "Satisfactory" },
        { value: 'NotSatisfactory', name: "Not Satisfactory" }];
        vm.Edit.Quality = vm.quality[0];

我已使用最后一行vm.Edit.Quality = vm.quality[0];将所选(默认)值设置为不起作用的质量。

2 个答案:

答案 0 :(得分:1)

you can use ng-option this way:

<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="Q.value as Q.name for Q in vm.quality" ng-model="vm.Edit.Quality" tooltip="Quality is required"
              tooltip-placement="top" required>
      <option value="?" selected="selected"></option>
      <option value="0">Item 1</option>
      <option value="1">Item 2</option>
      <option value="2">Item 3</option>
    </select>
 </div>

答案 1 :(得分:0)

试试这个

&#13;
&#13;
var app = angular.module('anApp', []);
app.controller('aCtrl', function($scope) {
var vm = this;
   vm.quality = [{ value: 'Satisfactory', name: "Satisfactory" },
        { value: 'NotSatisfactory', name: "Not Satisfactory" }];
        vm.Quality = vm.quality[0].value;
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>

<div ng-app="anApp" ng-controller="aCtrl as vm">
 <div class="form-group">
     <label class="control-label col-lg-2 pull-left">Quality<span class="Imp">*</span></label>
     <div>
      <select ng-options="Q.value as Q.name for Q in vm.quality"
                   ng-model="vm.Quality">
      </select>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;