当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > Angular.JS中select下拉框设置value的方法

Angular.JS中select下拉框设置value的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

近期电影上映,木易太,学校怪谈

前言

本文主要给大家介绍的是关于angular.js中select下拉框设置value的相关内容,非常出来供大家参考学习,下面来一起看看详细的介绍:

最近在系统中增加一个查询的筛选条件,通过下拉框选取,用的是angular常见的ng-options 指令:

<select id="selectdetectunit" class="form-control" ng-model="detectunits" ng-options="detectunit.name for detectunit in detectqueryfilters.detectunits"> 
   <option value="">全部</option> 
</select> 

但是有个问题,ng-options指令仅仅设置了下拉框选项的text,而不是value,打印下拉框的内容如下:

<option value="" class="">全部</option> 
<option value="0">董浜惠健净菜</option> 
<option value="1">古里绿品公司</option> 
<option value="2">曹家桥物流公司</option> 
<option value="3">董浜农服中心</option> 

value部分是自动设置的0,1,2,3,并不是实际的id。

那么,angualr js 怎样设置下拉框的value呢?

网上查了一遍,结合自己的一点探索,找到了答案,类似于表格记录的用法

<select id="selectdetectunit" class="form-control" ng-model="filter.detectunitid" > 
  <option value="">全部</option> 
 <option ng-repeat="detectunit in detectqueryfilters.detectunits" value="{{detectunit.id}}">{{detectunit.name}}</option> 
</select> 

打印下拉框的内容如下:

<option value="">全部</option>        
<!-- ngrepeat: detectunit in detectqueryfilters.detectunits --> 
<option ng-repeat="detectunit in detectqueryfilters.detectunits" value="160101" class="ng-scope ng-binding">董浜惠健净菜</option> 
<option ng-repeat="detectunit in detectqueryfilters.detectunits" value="160102" class="ng-scope ng-binding">古里绿品公司</option> 
<option ng-repeat="detectunit in detectqueryfilters.detectunits" value="160103" class="ng-scope ng-binding">曹家桥物流公司</option> 
<option ng-repeat="detectunit in detectqueryfilters.detectunits" value="160104" class="ng-scope ng-binding">董浜农服中心</option> 
<option ng-repeat="detectunit in detectqueryfilters.detectunits" value="160105" class="ng-scope ng-binding">港南村7组</option> 

虽然option中多了一些属性,看着有点复杂,不过value总算有了正确的值。

然后试着取值:

alert($scope.filter.detectunitid); 

问题解决!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网