选择选项上的角度ng-repeat正在生成一个空白选项

时间:2014-11-17 23:03:47

标签: angularjs angularjs-ng-repeat

鉴于此角度选择菜单:

<select
    ng-init="selectedItem = selecteditem || items[0]"
    ng-model="selectedItem">
    <option
    ng-repeat="item in items"
    ng-selected="item.id == selectedItem.id"
    ng-value="item.id">{{item.name}}</option>
</select>

它产生以下HTML:

<select class="ng-pristine ng-valid ng-touched" ng-init="selectedItem = selecteditem || items[0]" ng-model="selectedItem">
    <option value="? object:10 ?"></option>
    <option selected="selected" value="1" class="ng-binding ng-scope" ng-repeat="item in items" ng-selected="item.id == selectedItem.id" ng-value="item.id">a</option>
    <option value="2" class="ng-binding ng-scope" ng-repeat="item in items" ng-selected="item.id == selectedItem.id" ng-value="item.id">b</option>
</select>       

我出于某种原因得到了这个:

    <option value="? object:10 ?"></option>

控制器:

$scope.selectedItem;
$scope.items = [
    { id: 1, name: 'Foo' },
    { id: 2, name: 'Bar' }
]

我不知道如何摆脱它。

1 个答案:

答案 0 :(得分:0)

要初始化您的下拉列表,只需将其添加到您的控制器

$scope.items = [
    { id: 1, name: 'Foo' },
    { id: 2, name: 'Bar' }
];
$scope.selectedItem = $scope.items[0].id;

您的标记应该只是:

<select ng-model="selectedItem" ng-options="item.id as item.name for item in items">
</select>