为Angularjs ngOptions加上index解决方案
2016-04-09 00:00
197 查看
如何为Angular select的ngOptions像Angularjs的ngRepeat一样加上一个索引$index.
其实对于这个问题来说Angular本身并未提供$index之类的变量供使用。但是也不是说对于这个问题我们就没有解决方案。
把这个问题换成角度来看,我们所需要的就是js数组的下标,所以我们如果我们能够在对象上加入下标,使用表达式作为option的label就能解决了。
但是第一印象让我想起的是js数组本来就是一个key/value的对象,只是key为数组下标而已,所以有了如下之设计:
html:
js:
可是不幸的是如果对于JavaScript你若将他作为key/value对象那么key将是无序的所以,出现了无序的下标如下:
所以这样是无法解决的。还好博主还有一招,ngOptions支持Angularjs的filter,所以我们可以对数据源对象上加上一个order字段标示下标作为序号。并且你可以在一个2年前的Angular的issue中看到Angular已经fix issue,option会对数组进行按下标顺序生成。
html:
js:
这下option是按照有序的生成,最后我们终于能完美解决了,所以本文也将收尾。在最后在附上可运行的demoplnkr ngOptions index;
其实对于这个问题来说Angular本身并未提供$index之类的变量供使用。但是也不是说对于这个问题我们就没有解决方案。
把这个问题换成角度来看,我们所需要的就是js数组的下标,所以我们如果我们能够在对象上加入下标,使用表达式作为option的label就能解决了。
但是第一印象让我想起的是js数组本来就是一个key/value的对象,只是key为数组下标而已,所以有了如下之设计:
html:
<pre>{{ a | json }}</pre> <select ng-model="a" ng-options="value.field as getDesc1(key,value) for (key,value) in t"></select>
js:
$scope.getDesc1 = function(key, value) { return (parseInt(key, 10) + 1) + "->" + value.field; };
可是不幸的是如果对于JavaScript你若将他作为key/value对象那么key将是无序的所以,出现了无序的下标如下:
<select ng-model="a" ng-options="l.field as getDesc1(key,value) for (key,value) in t " class="ng-valid ng-dirty"> <option value="0" >1->jw_companyTalent</option> <option value="1" >2->jw_reportgroup</option> <option value="10" >11->jw_ads</option> <option value="11" >12->jw_jobcomment</option> <option value="12" >13->jw_companyInfo</option> .... </select>
所以这样是无法解决的。还好博主还有一招,ngOptions支持Angularjs的filter,所以我们可以对数据源对象上加上一个order字段标示下标作为序号。并且你可以在一个2年前的Angular的issue中看到Angular已经fix issue,option会对数组进行按下标顺序生成。
html:
<pre>{{ b | json }}</pre> <select ng-model="b" ng-options="l.field as getDesc(l) for l in t | index "></select>
js:
var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.t = [{ "field": "jw_companyTalent" }, { "field": "jw_reportgroup" }]; $scope.getDesc = function(l) { return l.order + "->" + l.field; }; }).filter("index", [ function() { return function(array) { return (array || []).map(function(item, index) { item.order = index + 1; return item; }); }; } ]);
这下option是按照有序的生成,最后我们终于能完美解决了,所以本文也将收尾。在最后在附上可运行的demoplnkr ngOptions index;
相关文章推荐
- angular controller as syntax vs scope
- Centos7下PHP安装gd库
- 六种微服务架构的设计模式
- ZooKeeper架构设计及其应用要点
- 基于Dubbo框架构建分布式服务
- Dubbo架构设计详解
- Open edX Devstak加快开发者模式调试速度
- Open edX官方提供5种方式扩展edx
- Open edXDevstack启动出现:could not connect to localhost:27017: [Errno 111] Connection refused.
- Open edX Devstack配置视频上传
- Open edX Cypress完整汉化语言包
- Open edX 学习、开发、运维相关链接整理
- Windows 8安装Open edX
- Open edx Fullstack安装及安装错误整理
- Open edX安装Xblock
- Open edX 简单查错
- 搭建Open edX经验总结
- Open edX修改自定义主题
- Open edX 56 网 XBlock 视频插件开发(基础)
- Open edX修改证书下载地址