我在这里复制了我的问题小提琴:http://jsfiddle.net/U3pVM/2840/
正如标题所示,我无法设置使用ng-options填充的选择的选定值。
我搜索并尝试了我找到的所有可能的解决方案。任何帮助或建议将不胜感激!
HTML
<div ng-app>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<select ng-model="ddlRooms" ng-options="r.id as r.Name for r in Rooms">
</select>
{{$scope.test}}
</div>
</div>
角
function TodoCtrl($scope) {
$scope.Rooms = [{ Name: 'Toddler', id: 1 },{ Name: 'other', id: 2 }];
$scope.options = [{ Name: 'other', id: 2 }];
$scope.ddlRooms = $scope.options[0];
$scope.test = 'bla';
}
答案 0 :(得分:16)
因为您正在执行id as name
,所以您需要为模型分配ID:
$scope.ddlRooms = $scope.options[0].id;
答案 1 :(得分:6)
更好:
<select ng-model="ddlRooms.id" ng-options="r.id as r.Name for r in Rooms">