预选< option>在< select>中在使用ng

编程入门 行业动态 更新时间:2024-10-26 17:21:05
本文介绍了预选< 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="ajax.googleapis/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>

更多推荐

预选&lt; option&gt;在&lt; select&gt;中在使用ng

本文发布于:2023-10-31 01:54:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:option   lt   amp   ng   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!