我已经搜索了谷歌,在这个上面什么也找不到。

我有这个代码。

<select ng-model="somethingHere" 
        ng-options="option.value as option.name for option in options"
></select>

有这样的数据

options = [{
   name: 'Something Cool',
   value: 'something-cool-value'
}, {
   name: 'Something Else',
   value: 'something-else-value'
}];

输出是这样的。

<select ng-model="somethingHere"  
        ng-options="option.value as option.name for option in options" 
        class="ng-pristine ng-valid">

    <option value="?" selected="selected"></option>
    <option value="0">Something Cool</option>
    <option value="1">Something Else</option>
</select>

如何将数据中的第一个选项设置为默认值,从而得到这样的结果呢?

<select ng-model="somethingHere" ....>
    <option value="0" selected="selected">Something Cool</option>
    <option value="1">Something Else</option>
</select>

当前回答

我需要默认的“请选择”是不可选择的。我还需要能够有条件地设置默认选择选项。

我通过以下简单的方法做到了这一点: JS代码: //翻转这两个按钮来测试所选的默认值或默认的“Please Select”文本 //$scope.defaultOption = 0; $scope.defaultOption = {key: '3', value: 'Option 3'};

$scope.options = [
   { key: '1', value: 'Option 1' },
   { key: '2', value: 'Option 2' },
   { key: '3', value: 'Option 3' },
   { key: '4', value: 'Option 4' }
];

getOptions();

function getOptions(){
    if ($scope.defaultOption != 0)
    { $scope.options.selectedOption = $scope.defaultOption; }
}

HTML:

<select name="OptionSelect" id="OptionSelect" ng-model="options.selectedOption" ng-options="item.value for item in options track by item.key">
<option value="" disabled selected style="display: none;"> -- Please Select -- </option>
</select>
<h1>You selected: {{options.selectedOption.key}}</h1>         

我希望这能帮助其他有类似需求的人。

“请选择”是通过Joffrey Outtier的回答完成的。

其他回答

这对我很管用。

<select ng-model="somethingHere" ng-init="somethingHere='Cool'">
    <option value="Cool">Something Cool</option>
    <option value="Else">Something Else</option>
</select>

使用select和ngOptions并设置默认值:

更多ngOptions使用示例请参见ngOptions文档。

angular.module('defaultValueSelect', []) .controller('ExampleController', ['$scope', function($scope) { $scope.data = { availableOptions: [ {id: '1', name: 'Option A'}, {id: '2', name: 'Option B'}, {id: '3', name: 'Option C'} ], selectedOption: {id: '2', name: 'Option B'} //This sets the default value of the select in the ui }; }]); <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script> <body ng-app="defaultValueSelect"> <div ng-controller="ExampleController"> <form name="myForm"> <label for="mySelect">Make a choice:</label> <select name="mySelect" id="mySelect" ng-options="option.name for option in data.availableOptions track by option.id" ng-model="data.selectedOption"></select> </form> <hr> <tt>option = {{data.selectedOption}}</tt><br/> </div>

plnkr.co

关于带有angular数据绑定的HTML SELECT元素的官方文档。

通过ngModel解析/格式化将select绑定到一个非字符串值:

(function(angular) { 'use strict'; angular.module('nonStringSelect', []) .run(function($rootScope) { $rootScope.model = { id: 2 }; }) .directive('convertToNumber', function() { return { require: 'ngModel', link: function(scope, element, attrs, ngModel) { ngModel.$parsers.push(function(val) { return parseInt(val, 10); }); ngModel.$formatters.push(function(val) { return '' + val; }); } }; }); })(window.angular); <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.1/angular.min.js"></script> <body ng-app="nonStringSelect"> <select ng-model="model.id" convert-to-number> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> {{ model }} </body>

plnkr.co

其他的例子:

angular.module('defaultValueSelect', []) .controller('ExampleController', ['$scope', function($scope) { $scope.availableOptions = [ { name: 'Apple', value: 'apple' }, { name: 'Banana', value: 'banana' }, { name: 'Kiwi', value: 'kiwi' } ]; $scope.data = {selectedOption : $scope.availableOptions[1].value}; }]); <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script> <body ng-app="defaultValueSelect"> <div ng-controller="ExampleController"> <form name="myForm"> <select ng-model="data.selectedOption" required ng-options="option.value as option.name for option in availableOptions"></select> </form> </div> </body>

斯菲德尔

如果你想确保你的$scope。当你的视图初始化时,somethingHere的值不会被覆盖,你会想要合并(somethingHere = somethingHere || options[0].value) ng-init中的值,如下所示:

<select ng-model="somethingHere" 
        ng-init="somethingHere = somethingHere || options[0].value"
        ng-options="option.value as option.name for option in options">
</select>

这对我很有用

ng-selected="true" 

您可以像这样简单地使用ng-init

<select ng-init="somethingHere = options[0]" 
        ng-model="somethingHere" 
        ng-options="option.name for option in options">
</select>