关于ng-options
在实际使用过程中对angular的ng-options指令有点不解,有的时候觉得很容易理解和上手,但其实等到遇到问题时,发现它很是生疏,(key,value)键值对获取,as关键词,track by 组合,还有group by,感觉里面应该有很多细的东西,而且还要搭配ng-model还有初始化值。等有时间慢慢研究一下,下面先记录刚刚碰到的问题:
group by使用时出现前面三个空白<optgroup label></optgroup>,
图1

图2

然后研究了一下代码:
//这是需要在界面显示的选项
$scope.funcFieldsSelect = [{"fieldName":"关闭","type":""},{"fieldName":"id","type":"INT"}] ;
因为默认选项是关闭状态,因为要按照type分组显示,所有的选项都保持一致性{"fieldName":"id","type":"INT"}格式,因此将type赋值为空。
然后在html中代码为
<select ng-model="selectedFields.funcFieldsSelected.fieldSelected" ng-options="funcVal.fieldName group by funcVal.type for funcVal in funcFieldsSelect">
</select>
然后显示为图1所示,保留了三个空白,后来尝试track by funcVal.fieldName
<select ng-model="selectedFields.funcFieldsSelected.fieldSelected" ng-options="funcVal.fieldName group by funcVal.type for funcVal in funcFieldsSelect track by funcVal.fieldName">
</select>
加上track by之后会少一个空白。
我怀疑空白是因为type:""导致的,后来发现可以这样改变一下,
$scope.funcFieldsSelect = ["关闭",{"fieldName":"id","type":"INT"}] ;
<select ng-model="selectedFields.funcFieldsSelected.fieldSelected" ng-options="(funcVal.fieldName||funcVal) group by funcVal.type for funcVal in funcFieldsSelect">
</select>
正常显示:

默认初始值可以设置为:

附官网ng-Options指令讲解:
select as and track by
items:
$scope.items = [{
id: 1,
label: 'aLabel',
subItem: { name: 'aSubItem' }
}, {
id: 2,
label: 'bLabel',
subItem: { name: 'bSubItem' }
}];
This will work:
<select ng-options="item as item.label for item in items track by item.id" ng-model="selected"></select>
$scope.selected = $scope.items[0];
but this will not work:
<select ng-options="item.subItem as item.label for item in items track by item.id" ng-model="selected"></select>
$scope.selected = $scope.items[0].subItem;
In both examples, the track by expression is applied successfully to each item in the items array. Because the selected option has been set programmatically in the controller, the track by expression is also applied to the ngModel value. In the first example, thengModel value is items[0] and the track by expression evaluates to items[0].id with no issue. In the second example, thengModel value is items[0].subItem and the track by expression evaluates to items[0].subItem.id (which is undefined). As a result, the model value is not matched against any <option> and the <select> appears as having no selected value.
in one of the following forms:
- for array data sources:
labelforvalueinarrayselectaslabelforvalueinarraylabelgroup bygroupforvalueinarraylabeldisable whendisableforvalueinarraylabelgroup bygroupforvalueinarraytrack bytrackexprlabeldisable whendisableforvalueinarraytrack bytrackexprlabelforvalueinarray| orderBy:orderexprtrack bytrackexpr(for including a filter withtrack by)
- for object data sources:
labelfor (key,value) inobjectselectaslabelfor (key,value) inobjectlabelgroup bygroupfor (key,value) inobjectlabeldisable whendisablefor (key,value) inobjectselectaslabelgroup bygroupfor(key,value) inobjectselectaslabeldisable whendisablefor(key,value) inobject
Where:
array/object: an expression which evaluates to an array / object to iterate over.value: local variable which will refer to each item in thearrayor each property value ofobjectduring iteration.key: local variable which will refer to a property name inobjectduring iteration.label: The result of this expression will be the label for<option>element. Theexpressionwill most likely refer to thevaluevariable (e.g.value.propertyName).select: The result of this expression will be bound to the model of the parent<select>element. If not specified,selectexpression will default tovalue.group: The result of this expression will be used to group options using the<optgroup>DOM element.disable: The result of this expression will be used to disable the rendered<option>element. Returntrueto disable.trackexpr: Used when working with an array of objects. The result of this expression will be used to identify the objects in the array. Thetrackexprwill most likely refer to thevaluevariable (e.g.value.propertyName). With this the selection is preserved even when the options are recreated (e.g. reloaded from the server).
<script>
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light', notAnOption: true},
{name:'red', shade:'dark'},
{name:'blue', shade:'dark', notAnOption: true},
{name:'yellow', shade:'light', notAnOption: false}
];
$scope.myColor = $scope.colors[2]; // red
}]);
</script>
<div ng-controller="ExampleController">
<ul>
<li ng-repeat="color in colors">
<label>Name: <input ng-model="color.name"></label>
<label><input type="checkbox" ng-model="color.notAnOption"> Disabled?</label>
<button ng-click="colors.splice($index, 1)" aria-label="Remove">X</button>
</li>
<li>
<button ng-click="colors.push({})">add</button>
</li>
</ul>
<hr/>
<label>Color (null not allowed):
<select ng-model="myColor" ng-options="color.name for color in colors"></select>
</label><br/>
<label>Color (null allowed):
<span class="nullable">
<select ng-model="myColor" ng-options="color.name for color in colors">
<option value="">-- choose color --</option>
</select>
</span></label><br/> <label>Color grouped by shade:
<select ng-model="myColor" ng-options="color.name group by color.shade for color in colors">
</select>
</label><br/> <label>Color grouped by shade, with some disabled:
<select ng-model="myColor"
ng-options="color.name group by color.shade disable when color.notAnOption for color in colors">
</select>
</label><br/> Select <button ng-click="myColor = { name:'not in list', shade: 'other' }">bogus</button>.
<br/>
<hr/>
Currently selected: {{ {selected_color:myColor} }}
<div style="border:solid 1px black; height:20px"
ng-style="{'background-color':myColor.name}">
</div>
</div>
推荐文章:http://www.undefinednull.com/2014/08/11/a-brief-walk-through-of-the-ng-options-in-angularjs/
关于ng-options的更多相关文章
- jquery photoClip支持手机端,PC端 本地裁剪图片后上传插件
支持手机,PC最好的是jquery photoClip插件,下载地址&示例:https://github.com/topoadmin/photoClip demo.html 代码: <! ...
- Flume NG Getting Started(Flume NG 新手入门指南)
Flume NG Getting Started(Flume NG 新手入门指南)翻译 新手入门 Flume NG是什么? 有什么改变? 获得Flume NG 从源码构建 配置 flume-ng全局选 ...
- Andrew NG 机器学习编程作业4 Octave
问题描述:利用BP神经网络对识别阿拉伯数字(0-9) 训练数据集(training set)如下:一共有5000个训练实例(training instance),每个训练实例是一个400维特征的列向量 ...
- Flume NG 配置详解(转)
原文链接:[转]Flume NG 配置详解 (说明,名词对应解释 源-Source,接收器-Sink,通道-Channel) 配置 设置代理 Flume代理配置存储在本地配置文件.这是一个文本文件格式 ...
- 斯坦福大学机器学习(Andrew Ng@2014)--自学笔记
今天学习Andrew NG老师<机器学习>之6 - 6 - Advanced Optimization,做笔记如下: 用fminunc函数求代价函数最小值,分两步: 1.自定义代价函数 f ...
- 【原】Coursera—Andrew Ng机器学习—编程作业 Programming Exercise 4—反向传播神经网络
课程笔记 Coursera—Andrew Ng机器学习—课程笔记 Lecture 9_Neural Networks learning 作业说明 Exercise 4,Week 5,实现反向传播 ba ...
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 4) Neural Networks Representation
Andrew NG的Machine learning课程地址为:https://www.coursera.org/course/ml 神经网络一直被认为是比较难懂的问题,NG将神经网络部分的课程分为了 ...
- Andrew Ng 的 Machine Learning 课程学习 (week4) Multi-class Classification and Neural Networks
这学期一直在跟进 Coursera上的 Machina Learning 公开课, 老师Andrew Ng是coursera的创始人之一,Machine Learning方面的大牛.这门课程对想要了解 ...
- [C2P2] Andrew Ng - Machine Learning
##Linear Regression with One Variable Linear regression predicts a real-valued output based on an in ...
- 在库中使用schematics——ng add与ng update
起步 创建一个angular库 ng new demo --create-application=false ng g library my-lib 可见如下目录结构 ├── node_modules ...
随机推荐
- Java中的继承性特性
继承性是java中的第二特性之一.而继承性最为关键的地方为:代码重用性的问题,利用继承性可以从已有的类中继续派生出新的子类,也可以利用子类扩展出更多的操作功能. 继承性的实现代码为:class 子类 ...
- javaweb中重定向和请求转发(response.sendRedirect()和request.getRequestDispatcher(rul).forward(request,response)))的区别
先来两张图,方便理解: 可以看出,重定向时,是服务器向游览器重新发送了一个response命令,让游览器再次向url2发送请求,以获取url2的资源 而请求转发时,类似于是服务器自己向自己发了一个跳转 ...
- linux(十)配置ssh免密登录实现
知道ssh的朋友应该知道它是用来干什么的,如果你不知道什么是ssh远程登录的话,可以去看一下我的上一篇博客,关于linux的网络基础的知识.备注:ssh是用于远端登入.执行ssh指令开启终端机阶段作业 ...
- MatplotLib常用基本操作
本文记录matlibplot常用基本操作,都是基本功能,不涉及复杂联合操作,其中各用法详细用法可参考官网: 1. 基本画图操作 ##mofan_matplotlib.pyplot import mat ...
- linux(十三)之磁盘分区、创建文件系统、挂载
前面学习了linux的用户管理 ,感觉是不是多lnux的多用户多任务的系统感觉十分了解了,但是其实并不然的.你还需要了解更多.接下来给大家分享的是 在vmware中添加硬盘创建分区,然后挂载到指定目录 ...
- mock server 前后端分离小结(转)
1.前台开发受限于后台开发的速度 2.后台提供给前台的接口缺字段,时常需要联调 基于Mock Server开发的好处: 对于前端来讲,可以独立于后端进行开发. 对于后端来讲,根据前端定义的接口进行开发 ...
- pythonl练习
练习:用户输入姓名.年龄.工作.爱好 ,然后打印成以下格式 ------------ info of Egon ----------- Name : Egon Age : 22 Sex : male ...
- 理解oracle中连接和会话
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp44 理解oracle中连接和会话 1. 概念不同:概念不同: 连接是指物 ...
- Mysql介绍和实践总结
本文首先介绍mysql的安装和基本使用.进阶操作.讲解mysql的导入导出和自动备份,然后介绍安全模式修改密码和mysql的全文本搜索功能,最后记录了个人使用mysql中遇到的问题集. 开始安装: s ...
- java伪代码
愚公移山的目标是毕力平险,指通豫南,达于汉阴,方法是扣石垦壤,箕畚运于渤海之尾 条件判断if(愚公死了)我的儿子替我完成.循环结构是“子又生孙,孙又生子,子子孙孙无穷匮也” import.java.大 ...