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

我有这个代码。

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

当前回答

简单地使用ng-selected="true",如下所示:

<select ng-model="myModel">
        <option value="a" ng-selected="true">A</option>
        <option value="b">B</option>
</select>

其他回答

我会在控制器中设置模型。然后选择将默认为该值。例: 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
  }
});

我认为,在包含'track by'之后,你可以在ng-options中使用它来得到你想要的东西,就像下面这样

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

这种方法更好因为当你想用对象列表替换字符串列表时你只需要把这个改成

 <select ng-model="somethingHere" ng-options="object.name for option in options track by object.id" ></select>

这里当然是一个带有属性名和id的对象。请注意,'as'不能用这种方式来表示ng-options,因为它只会设置值,当您使用track by时,您将无法更改它

如果你有一些东西,而不仅仅是init日期部分,你可以使用ng-init()通过在你的控制器中声明它,并在你的HTML顶部使用它。 这个函数将像控制器的构造函数一样工作,你可以在那里初始化变量。

angular.module('myApp', [])
 .controller('myController', ['$scope', ($scope) => {
   $scope.allOptions = [
     { name: 'Apple', value: 'apple' }, 
     { name: 'Banana', value: 'banana' }
   ];
   $scope.myInit = () => {
      $scope.userSelected = 'apple'
      // Other initiations can goes here..
   }
}]);


<body ng-app="myApp">
  <div ng-controller="myController" ng-init="init()">
    <select ng-model="userSelected" ng-options="option.value as option.name for option in allOptions"></select>
   </div>
</body>

Srivathsa Harish Venkataramana只有一个回答提到了轨道,这确实是一个解决方案!

下面是一个关于如何在select ng-options中使用track by的例子(链接如下):

<select ng-model="selectedCity"
        ng-options="city as city.name for city in cities track by city.id">
  <option value="">-- Select City --</option>
</select>

如果selectedCity定义在angular作用域上,并且它的id属性与cities列表中任何城市的id值相同,它将在加载时自动选择。

下面是普伦克的解释: http://plnkr.co/edit/1EVs7R20pCffewrG0EmI?p=preview

有关更多细节,请参阅源代码文档: https://code.angularjs.org/1.3.15/docs/api/ng/directive/select

对于Ben Lesh的回答,应该有这样一条线

ng-init="somethingHere = somethingHere || options[0]" 

而不是

ng-init="somethingHere = somethingHere || options[0].value" 

也就是说,

<选择ng-model = " somethingHere " ng-init="somethingHere = somethingHere || options[0]" Ng-options ="option.name for options track by option.value"> < /选择>