ng-option默认值始终为空

时间:2014-08-11 00:18:14

标签: angularjs ng-options

我不能为我的生活设置我的选择的默认值。它一直是空白的。啊。

<div ng-controller="quickComposeController">
    <div id="QuickComposeEmail">
        <form id="quickComposeForm">
            <div class="modal-header">
                <h3>Set email preferences</h3>
            </div>
            <div class="modal-body">
                <select ng-init="selected=selected[0]" ng-model="selected" ng-options="o.value as o.name for o in optionsObject">

                </select>
            </div>
            <div class="modal-footer">
                <input class="btn btn-success" type="submit" value="Compose Email">
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
            </div>
        </form>
    </div>
</div>

这是我的控制人员:

app.controller('quickComposeController', ['$scope', function($scope){

    $scope.selected = [{name : "Use Default Mail Client", value : "mailto"}];

    $scope.optionsObject = [
        {name : "Use Default Mail Client", value : "mailto"},
        {name : "Gmail", value : "gmail"},
        {name : "Yahoo", value : "yahoo"},
        {name : "Hotmail", value : "hotmail"},
        {name : "AOL", value : "aol"}
    ]

}]);

我只是在底部写下这个,因为stackoverflow告诉我我的帖子主要是代码,我需要更多文本。每个人都度过美好的一天!

1 个答案:

答案 0 :(得分:2)

您的选择应该是:

<select ng-init="selected=selected[0].value" ng-model="selected" ng-options="o.value as o.name for o in optionsObject">

这里是associated plunkr

需要注意的是,您必须不仅指定正确的索引,还要指定其中的value属性,因为您的选项基于value