AngularJS - 设置下拉列表的选定值不起作用

时间:2014-02-04 06:54:40

标签: angularjs select set selecteditem ng-options

我在这里复制了我的问题小提琴: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';
}

2 个答案:

答案 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">