预选< option>在< select>中在使用ng-repeat时

问题描述:

我正在使用AngularJS和HTML.如果使用ng-repeat生成我的选项,是否可以在选项标签中预先选择一个选项?例如,请参见下面的代码片段:

I am using AngularJS and HTML. Is there a way to pre-select an option in an option tag if my options are being generated using an ng-repeat? See below code snippet for example:

HTML:

<select class="form-control" id="eventSelectMenu">
    <option ng-repeat="option in allEventData">{{option.name}}</option>
</select>

我已尝试在此处中提出的解决方案此处,但没有已经成功了.

I have tried solutions presented here and here, but none have been successful.

感谢您提供的任何建议!

Thank you for any advice you can give!

您可以设置任何应预先选择为select的 ngModel

you can set any item which should be pre-selected as value of select's ngModel

console.clear();
angular.module('so-answer', [])
  .controller('ctrl', ['$scope',
    function($scope) {
      $scope.options = [1, 2, 3, 4, 5, 6];
      $scope.selected = 3;
    }
  ])

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="so-answer" ng-controller="ctrl">
  <select ng-model="selected" ng-options="opt for opt in options"></select>
</div>