【问题标题】:AngularJS Select element set the selected indexAngularJS Select 元素设置选中的索引
【发布时间】:2014-10-06 13:16:44
【问题描述】:

所以我使用 angular,这是我选择的 html:

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

这是在我的控制器中:

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

结果:三个选项:一个空白(默认选中),然后是我在html中做的两个选项

什么鬼?为什么当我打开视图“另一个选项”时不是默认的

【问题讨论】:

标签: javascript html angularjs default selectedindex


【解决方案1】:

首先,请务必查看官方文档。 AngularJs 有据可查。

其次,不要使用document.getElementById ("date") selectedIndex = "0" - 那是javascript。当 Angular 函数可用时,避免使用纯 Javascript。

文档:

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

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

【讨论】:

  • 谢谢! ng-selected="selectedOption" in controller: selectedOption = "another option" 成功了!
猜你喜欢
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多