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

我有这个代码。

<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的回答完成的。

其他回答

试试这个:

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;

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

对于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"> < /选择>

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

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

接受的答案使用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。

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

这对我很有用

ng-selected="true"