我在SO上关于ngOptions和文档的帖子很少(1,2
)。我正在使选择正常工作,但在首次启动时选择默认值时出现问题。一旦我改变它,一切都很完美。当this questoin正确回答时,angularjs会在提供的选项中找不到模型值时创建一个空选项,但我的值3存在于提供的范围内。
下面是我从服务器收到的sList对象,但是select总是以空选择呈现非选择,而模型中存在有效值。这可能是因为'3'和3之间存在差异,但我不确定是否以及如何产生差异。
在JS中:
sModel = 3;
sList = {0: "NONE", 1: "LS", 2: "SS", 3: "CR"};
<select ng-model="sModel" ng-options="key as val for (key,val) in sList track by key"></select>
渲染选择框:
<select ng-model="sModel" ng-options="key as val for (key,val) in sList track by key" class="ng-pristine ng-valid ng-not-empty ng-touched" aria-invalid="false" style="">
<option value="?" selected="selected"></option>
<option label="NONE" value="0">NONE</option>
<option label="LS" value="1">LS</option>
<option label="SS" value="2">SS</option>
<option label="CR" value="3">CR</option>
</select>
我知道大多数此类问题都受到重复答案的保护,但我找不到我遇到的确切问题,所以这可能是我遗失的基本问题。
任何有助于了解我在哪里做错的帮助都非常感谢。
答案 0 :(得分:0)
var myapp = angular.module('app', []);
myapp.controller('Ctrl', function ($scope) {
var vm = this;
vm.sModel = "3";
vm.sList = {0: "NONE", 1: "LS", 2: "SS", 3: "CR"};
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl as vm">
<select ng-model="vm.sModel" ng-options="key as val for (key,val) in vm.sList"></select>
</div>
&#13;
试试这个
sModel = "3";
sList = {0: "NONE", 1: "LS", 2: "SS", 3: "CR"};