选择不显示其默认值

Select not displaying its default value

本文关键字:默认值 显示 选择      更新时间:2023-09-26

我有一个select,其值是一个简单对象。

显示为option的内容是该对象的属性,但值是该对象本身。

我通过将模型设置为适当的对象来设置此select的默认值。

select没有显示任何选择值,而模型设置正确。

以下是我尝试过的三种不同的方法:
Current value of representative.title : {{representative.title}} <br/><br />
<div class="form-group">
  <select ng-model="representative.title" 
          ng-options="title.name for title in titles" 
          ng-disabled="$parent.isDisabled" 
          class="form-control">
  </select>
</div>
<br />
<div class="form-group">
  <select ng-model="representative.title" 
          class="form-control">
    <option ng-repeat="title in titles" 
            value="{{title}}">
              {{title.name}}
    </option>
  </select>
</div>
<br />
<div class="form-group">
  <select ng-model="representative.title" 
          ng-options="title as title.name for title in titles" 
          ng-disabled="$parent.isDisabled" 
          class="form-control">
  </select>
</div>

在我的控制器中:

$scope.titles = [{"name": "Monsieur"},{"name": "Madame"}];
$scope.representative = {"title": {"name": "Monsieur"}, "otherField": "otherValue"};

为什么我的select不显示默认值?

如何解决这个问题?

(顺便说一下,我使用的是Angular 1.2.28)

$scope.representative = {"title": $scope.titles[0], "otherField": "otherValue"};

如果您需要传递选项数组中包含的对象之一的引用,则

会更准确。