今天在Angularjs交流群中有位童学问道如何为Angular select的ngOptions像Angularjs的ngRepeat一样加上一个索引$index.

其实对于这个问题来说Angular本身并未提供$index之类的变量供使用。但是也不是说对于这个问题我们就没有解决方案。

把这个问题换成角度来看,我们所需要的就是js数组的下标,所以我们如果我们能够在对象上加入下标,使用表达式作为option的label就能解决了。

但是第一印象让我想起的是js数组本来就是一个key/value的对象,只是key为数组下标而已,所以有了如下之设计:

html:

<pre></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-&gt;jw_companyTalent</option>
<option value="1" >2-&gt;jw_reportgroup</option>
<option value="10" >11-&gt;jw_ads</option>
<option value="11" >12-&gt;jw_jobcomment</option>
<option value="12" >13-&gt;jw_companyInfo</option>
....
</select>

所以这样是无法解决的。还好博主还有一招,ngOptions支持Angularjs的filter,所以我们可以对数据源对象上加上一个order字段标示下标作为序号。并且你可以在一个2年前的Angular的issue中看到Angular已经fix issue,option会对数组进行按下标顺序生成。

html:

<pre></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是按照有序的生成,最后我们终于能完美解决了,所以本文也将收尾。在最后在附上可运行的demo plnkr ngOptions index;

为Angularjs ngOptions加上index解决方案的更多相关文章

  1. angularjs跨域post解决方案

    转自:http://www.thinksaas.cn/topics/0/34/34536.html 前端同学李雷和后台同学韩梅梅分别在自己电脑上进行开发,后台接口写好的时候,李雷改动完就把前端代码上传 ...

  2. angularjs ng-option ie issue解决方案

    最近遇见angularjs 在IE上当使用ng-options作为select的选项数据源,并且被套在ng-switch(ng-transclude)之类的,当angular上得ng-options数 ...

  3. [转]Ionic + AngularJS angular-translate 国际化本地化解决方案

    本文转自:http://www.cnblogs.com/maoyazhi/p/4332362.html 欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术 ...

  4. Ionic + AngularJS angular-translate 国际化本地化解决方案

    欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术分享/ionic-plus-angularjs-angular-translate-国际化本地化解决方案 ...

  5. AngularJS track by $index引起的思考

    今天写了一段程序,只是一个简答的table数据绑定,但是绑定select的数据之后,发现ng-change事件失去了效果,不知道什么原因. 主要用到的代码如下: <div id="ri ...

  6. How to set the initial value of a select element using AngularJS ng-options & track by

    原文: https://www.gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-element ...

  7. AngularJS 相关小问题解决方案合集

    1  解决 Select选择框遍历时,出现一个空白选项: <select style="width: 20%;margin-left: 5px;height: 31px;" ...

  8. 怎么解决dede首页网址自动加上index.html

    怎样去掉dedecms5.7(织梦)首页url后index.html有三种方法 1.去配置你的空间的默认首页地址.把index.html移到默认文本最前面.(确保你的默认文档里面有index.html ...

  9. 随笔编号-04 AngularJS 相关小问题解决方案合集

    1  解决 Select选择框遍历时,出现一个空白选项: <select style="width: 20%;margin-left: 5px;height: 31px;" ...

随机推荐

  1. vs2015 command prompt here

    网上搜的很多方法都不能用,比如:http://app.paraesthesia.com/CommandPromptHere/ 主要是都搞错了注册表路径,写成了: HKCR,Directory\Shel ...

  2. springMVC 相对于 Structs 的优势

    智者说,没有经过自己的思考和估量,就不能接受别人的东西.资料只能是一个参考,至于是否正确,还得自己去分辨 SpringMVC相对于Structs的几个优势: 1.springMVC安全性更高,stru ...

  3. [OSI]网络7层模型的理解

    应用层:HTTP 应用层 表现层:编码 表现层 会话层:端口 会话层 传输层:TCP/UDP 协议 传输层 网络层:IP 标记  +-- 网络层 --+  网络层 数据链路层:计算机Mac地址标记 | ...

  4. C# 反转字符串

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 实现字符 ...

  5. spring知识大全(3)

    4 Spring对持久层的支持 Spring对持久层的支持:① JDBC,② O/R Mapping(Hibernate,TopLink等) 一.Spring对持久层支持采用的策略: 1.Spring ...

  6. 使用AOP框架所需引入的Jar包

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  7. error while performing database login with the xxx driver

    在MyEclipse的安装路径下D:\Program Files\MyEclipse 6.0\eclipse下面找到eclipse.ini文件,用记事本打开 eclipse.ini文件 -showsp ...

  8. SQLServer数据库中创建临时表

    IF object_id('tempdb..#jimmy') is not NULL BEGIN DROP TABLE #jimmy; END IF object_id('tempdb..#jimmy ...

  9. 【13-Annotation】

    Annotation 5个基本的Annotation •@Override •@Deprecated •@SuppressWarnings •@SafeVarargs •@FunctionalInte ...

  10. IIS 部署 node.js ---- 基础安装部署

    一些可能有用的相关文章: https://blogs.msdn.microsoft.com/scott_hanselman/2011/11/28/window-iisnode-js/ http://b ...