我正在使用 AngularJS 和 HTML。如果我的选项是使用 ng-repeat 生成的,有没有办法在选项标签中预先选择一个选项?例如,请参阅下面的代码片段:
HTML:
<select class="form-control" id="eventSelectMenu">
<option ng-repeat="option in allEventData">{{option.name}}</option>
</select>
我已经尝试过提供的解决方案 here和 here ,但都没有成功。
感谢您提供的任何建议!
最佳答案
您可以设置任何应预先选择为 select 的 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>
关于javascript - 使用 ng-repeat 时在 <select> 中预选 <option>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38731116/