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

我有这个代码。

<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>

当前回答

使用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>

斯菲德尔

其他回答

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

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

斯菲德尔

接受的答案使用ng-init,但是文档说如果可能的话避免ng-init。

ngInit唯一合适的用法是对特殊属性进行混叠 ngRepeat,如下图所示。除了这个案子,你应该 使用控制器而不是ngInit来初始化作用域上的值。

你也可以使用ng-repeat来代替ng-options。使用ng-repeat,您可以使用ng-selected和ng-repeat的特殊属性。例如,$index, $odd, $even使其无需任何编码即可工作。

$first是ng-repeat的一个特殊属性。

  <select ng-model="foo">
    <option ng-selected="$first" ng-repeat="(id,value) in myOptions" value="{{id}}">
      {{value}}
    </option>
  </select>

----------------------编辑---------------- 虽然这是可行的,但我更喜欢@mik-t的答案,当您知道要选择什么值时,https://stackoverflow.com/a/29564802/454252,它使用track-by和ng-options,而不使用ng-init或ng-repeat。

这个答案只适用于当你必须选择第一项而不知道选择什么值时。例如,我正在使用这个自动完成,它需要一直选择第一个项目。

我的解决方案是使用html硬编码我的默认选项。像这样:

HAML:

%select{'ng-model' => 'province', 'ng-options' => "province as province for province in summary.provinces", 'chosen' => "chosen-select", 'data-placeholder' => "BC & ON"}
  %option{:value => "", :selected => "selected"}
    BC &amp; ON

在HTML中:

<select ng-model="province" ng-options="province as province for province in summary.provinces" chosen="chosen-select" data-placeholder="BC & ON">
  <option value="" selected="selected">BC &amp; ON</option>
</select>

我想我的默认选项从我的api返回所有的值,这就是为什么我有一个空白值。也请原谅我的笨手笨脚。我知道这不是对OP问题的直接回答,但人们在谷歌上找到了这个。希望这能帮助到其他人。

我会在控制器中设置模型。然后选择将默认为该值。例: html:

<select ng-options="..." ng-model="selectedItem">

Angular控制器(使用资源):

myResource.items(function(items){
  $scope.items=items;
  if(items.length>0){
     $scope.selectedItem= items[0];
//if you want the first. Could be from config whatever
  }
});