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

我有这个代码。

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

当前回答

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

其他回答

我认为最简单的方法是

 ng-selected="$first"

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

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

在你的角控制器中试试这个…

$somethingHere = {name: 'Something Cool'};

你可以设置一个值,但你使用的是复杂类型,angular会在视图中搜索key/value来设置。

如果不奏效,试试这个: ng-options = "选项。值为option.name的选项跟踪option.name"

在我的情况下,因为默认情况下不同的情况下,在形式。 我在选择标记中添加了一个自定义属性。

 <select setSeletected="{{data.value}}">
      <option value="value1"> value1....
      <option value="value2"> value2....
       ......

在指令中,我创建了一个检查值的脚本,当angular填充它时,将该值设置为选中的选项。

 .directive('setSelected', function(){
    restrict: 'A',
    link: (scope, element, attrs){
     function setSel=(){
     //test if the value is defined if not try again if so run the command
       if (typeof attrs.setSelected=='undefined'){             
         window.setTimeout( function(){setSel()},300) 
       }else{
         element.find('[value="'+attrs.setSelected+'"]').prop('selected',true);          
       }
     }
    }

  setSel()

})

刚刚从coffescript中翻译了这个,如果不是洞的东西,至少它的jist是正确的。

这不是最简单的方法,但当值变化时就能完成

试试这个:

HTML

<select 
    ng-model="selectedOption" 
    ng-options="option.name for option in options">
</select>

Javascript

function Ctrl($scope) {
    $scope.options = [
        {
          name: 'Something Cool',
          value: 'something-cool-value'
        }, 
        {
          name: 'Something Else',
          value: 'something-else-value'
        }
    ];

    $scope.selectedOption = $scope.options[0];
}

砰砰作响。

如果您真的想设置将绑定到模型的值,那么将ng-options属性更改为

ng-options="option.value as option.name for option in options"

和Javascript

...
$scope.selectedOption = $scope.options[0].value;

考虑到上述情况,这里又一个普伦克。