angularjs select required not working ng-options

两盒软妹~` 提交于 2019-12-03 14:37:53

问题


I am trying to ensure a user selects a value from a select box before submitting the form. This works correctly with static options; it however fails when populating the options via a model using ng-options

Examplehttp://plnkr.co/edit/wRqSCNskLo5c48jy4nQf

I am using AngularJS 1.2.9

Thank you in advance.


回答1:


In your example changing the data bound select to the following fixes the required directive. I'm not sure why exactly.

<select ng-model="selectedValue1" ng-options="opt for opt in ['Mazda2','Maxda3']" required>
  <option></option>
</select>



回答2:


You can also try ng-required="true" (it works on the latest AngularJS, not sure for 1.2.x).

<select ng-model="selectedValue1" ng-options="opt for opt in ['','Mazda2','Maxda3']" ng-required="true"></select>


来源:https://stackoverflow.com/questions/24921605/angularjs-select-required-not-working-ng-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!