简体   繁体   English

为角度下拉菜单设置选定的值

[英]Set selected value for a dropdown in angular

I'm working in an angularjs app, and I having a hard time trying to set the selected value in a select tag. 我正在使用angularjs应用程序,但很难尝试在select标签中设置所选值。

Notice, the 2nd option has the selected="selected" 注意,第二个选项具有selected="selected"

I tried all the solutions posted, like set the doc type t html5, add autocomplete=off , add a name to the form tag. 我尝试了所有发布的解决方案,例如将文档类型设置为html5,添加autocomplete=off ,为表单标签添加名称。 And nothing seems to work. 似乎没有任何工作。 Any idea how to achieve that. 任何想法如何实现这一目标。

This is fiddle: https://jsfiddle.net/20cw7eqe/ and works fine there. 这是小提琴: https : //jsfiddle.net/20cw7eqe/,并且在这里可以正常工作。

But it does not work on FF or Chrome. 但它不适用于FF或Chrome。 BTW, Im using bootstrap. 顺便说一句,我正在使用引导程序。

Any workaround? 任何解决方法?

Take a look at this example: http://plnkr.co/edit/Xc8Qrcr0AN4I2U0chbye?p=preview 看一下这个例子: http : //plnkr.co/edit/Xc8Qrcr0AN4I2U0chbye?p=preview

There you can see 2 different examples of select tag, for different models. 在那里,您可以看到2个不同型号的select标签示例。 You can use the one that is more similar to your case. 您可以使用与您的案例更相似的一种。

Just set in your controller the value you want to be selected. 只需在控制器中设置要选择的值即可。 ng-model will make the rest. ng-model将完成其余的工作。

I have edited the plunker so you can se how ng-selected works. 我已经编辑了插件,因此您可以了解ng-selected的工作方式。 This was the code included: 这是包含的代码:

<select required="true" ng-model="selected3">
    <option ng-repeat="option in options2" ng-selected="option == 'second'" >{{option}}</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM