预选< option>在< select>中在使用ng-repeat时 [英] Preselect <option> in <select> while using ng-repeat
本文介绍了预选< 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>
这篇关于预选< option>在< select>中在使用ng-repeat时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文