AngularJS Select元素设置选定的索引 [英] AngularJS Select element set the selected index

查看:480
本文介绍了AngularJS Select元素设置选定的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < select id =dateng- model =selectedDayng-change =setDate()> 
< option>另一个选项< / option>
< option>一个选项< / option>
< / select>

这是在我的控制器中:

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

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



到底是什么?为什么不是默认的当我打开视图另一个选项

解决方案

首先,总是检查官方文档。 AngularJs是有据可查的。



其次,不要使用 document.getElementById(date)selectedIndex =0 - 这是JavaScript。



文档:



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



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


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"

解决方案

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

这篇关于AngularJS Select元素设置选定的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆