javascript - 选择不显示其默认值

标签 javascript html angularjs html-select

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

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

我通过将模型设置为正确的对象来为此选择设置默认值。

但是,当模型设置正确时,选择不会显示任何选定的值。

以下是我尝试过的 3 种不同方法:

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>

在我的 Controller 中:

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

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

如何修复它?

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

最佳答案

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

这会更准确,因为您需要传递选项数组中包含的对象之一的引用。

关于javascript - 选择不显示其默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39165252/

相关文章:

javascript - 如何通过 Javascript 创建 WinJS Flyout

java - 我的表单中存在验证错误

html - 嵌套的 ng-repeat 在 IE8 中不起作用

javascript - AngularJS 中的多个 View 、多个解析但针对一个 Controller

javascript - 更新 $scope 不影响 View

javascript - 检查是否所有值都是字段

javascript - 无论我把它放在哪里,Onload 都会触发

html - 如何使用 Bootstrap 中的按钮移至页面底部

javascript - 如何使用 for 循环在 React 中的 map 上渲染标记?

javascript - 模数在 Javascript 中不起作用