AngularJS Select element set the selected index

你。 提交于 2019-12-06 03:42:59

问题


So i'm using angular and this is my select html:

<select id="date" ng-model="selectedDay" ng-change="setDate()" >
    <option>another option</option>
    <option>an option</option>
</select>

this is in my controller:

$scope.selectedDay;
document.getElementById("date").selectedIndex = "0";

The result: Three options: one blank (which is default selected) and then the two options I made in html

What the hell? why isn't the default when i open the view "another option"


回答1:


Firstly, always check the official documentation. AngularJs is well documented.

Secondly, do not use document.getElementById ("date") selectedIndex = "0" - that's javascript. Avoid using pure Javascript when an Angular function is available.

Documentation:

https://docs.angularjs.org/api/ng/directive/select

https://docs.angularjs.org/api/ng/directive/ngSelected



来源:https://stackoverflow.com/questions/26217067/angularjs-select-element-set-the-selected-index

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