我有一个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/