AngularJS进阶(四)ANGULAR.JS实现下拉菜单单选
ANGULAR.JS: NG-SELECT AND NG-OPTIONS
PS:其实看英文文档比看中文文档更容易理解,前提是你的英语基础还可以。英文文档对于知识点讲述简明扼要,通俗易懂,而有些中文文档读起来特别费力,基础差、底子薄的有可能一会就会被绕晕了,最起码英文文档中的代码与中文文档中的代码是一致的,但知识点讲述实在是差距太大。
Angular.js has a powerful directive waiting for you: it's ng-select. With it you can fill an HTML-select box from your model. It supports a pretty cool selector syntax, which is - unfortunately - a little bit tricky.
Let's assume you have a JSON stream which looks like this:
{
"myOptions": [
{
"id": 106,
"group": "Group 1",
"label": "Item 1"
},
{
"id": 107,
"group": "Group 1",
"label": "Item 2"
},
...
}
// Get stream in the object called data
$scope.myOptions = data.myOptions;
The good thing is, you have a pretty flat data stream. When I started with this feature, I had an array of groups each containing the specific items.
It was not possible for me to fill a select box that way. Instead, I refactored some of my code to what you can see above.
Angular.js would take care of the grouping on it's own.
Here is my select definition:
<select
ng-model="myOption"
ng-options="value.id as value.label group by value.group for value in myOptions">
<option>--</option>
</select>
ng-model is the name of the property which will reference the selected option. ng-options is the directive which fills the dropdown. It deserves a bit more attention.
You will understand it more easily if you read it from right to left. First there is:
for value in myOptions
It means you'll iterate through elements which are stored in myOptions. Every element will become available in this expression with the name "value".
The next part is:
group by value.group
This will tell Angular.js to make up
<optgroup>
tags and the label attribute will be filled with the content of the group field.
The last one is:
value.id as value.label
In this case, value.id will become the model (stored in ng-model), if your users have chosen an option. If you would delete "value.id as", simply the whole value object would become the model.
value.label
does exactly what it looks like: it's the label of the select box.
If you run your code, you'll see something like that:
<optgroup label="Group 1">
<option value="0">Item 1</option>
<option value="1">Item 2</option>
</optgroup>
Please look again and check the value attribute of the options. You might have expected it's matching the IDs from your JSON,
but that is not the case (and yes, I thought like this initially). Actually this is an increasing number and references the position of the model
(which is an array in my case). Don't worry about that - if you select your option the correct ID will be selected and put into your model.
Or, if you leave out the value.id part of the expression, the whole selected object will become your model.You can easily test it.
<select
ng-change="selectAction()"
ng-model="myOption"
ng-options="value.id as value.label group by value.group for value in myOptions">
<option>--</option>
</select>
ng-change will fire if the user has chosen something. You can output it by typing:
$scope.selectAction = function() {
console.log($scope.myOption);
};
I find the syntax of ng-options pretty much counter intuitive. It took me a good while to understand it and I was glad about the nice help on the AngularJS mailinglist.
That said, I think more examples on the docs and an improved syntax would really help. Like:
foreach value in myOptions use value.label as label use value.id as model group by value.group
Here is a working JS fiddle example which illustrates the use of the ng-select directive: http://jsfiddle.net/jm6of9bu/2/
AngularJS进阶(四)ANGULAR.JS实现下拉菜单单选的更多相关文章
- 纯css和js版下拉菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- js 联动下拉菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- js版本下拉菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- JS实现下拉菜单的功能
<!DOCTYPE html> <html> <head> <meta charset = "utf8"> <title> ...
- js获取下拉,单选
1.JS取下拉框的显示值和判断单选按钮 1.需要得到select组件显示的值.下面是经常用到的方法: Html 源码: <html><body><select id=&q ...
- 第二百四十四节,Bootstrap下拉菜单和滚动监听插件
Bootstrap下拉菜单和滚动监听插件 学习要点: 1.下拉菜单 2.滚动监听 本节课我们主要学习一下 Bootstrap 中的下拉菜单插件,这个插件在以组件的形式我们 已经学习过,那么现在来看看怎 ...
- 关于Eclipse插件开发(四)-------给视图加下拉菜单和按钮和加入编辑器.
本例将给视图加入下拉菜单和按钮,同时再为列表添加一个右键菜单. 创建ActionGroup类 加入菜单和按钮的方法与SWT和JFace组件的一样,先创建一个ActionGroup代码如下: MyAct ...
- js模拟下拉菜单-键盘、鼠标(代码详解)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- bootstrap和JS实现下拉菜单
// bootstrap下拉菜单 <div class="btn-group"> <button id="button_text" type= ...
随机推荐
- Java异常处理-----Throwable类
Throwable类 1.toString() 输出该异常的类名. 2.getMessage() 输出异常的信息,需要通过构造方法传入异常信息(例如病态信息). 3.printStackTrace() ...
- Redis 学习笔记4: Redis 3.2.1 集群搭建
在CenOS 6.7 linux环境下搭建Redis 集群环境 1.下载最新的Redis版本 本人下载的Redis版本是3.2.1版本,下载之后,解压,编译(make): 具体操作可以参考我的博文:R ...
- SpriteKit关于SKScene中的渲染Loop
在本节中,我将来说明一下SKScene在SKView显示之后发生了神马. 在更传统的iOS app中,你可能只会渲染view的内容仅仅一次,然后它将保持静态直到view的模式发生了显示的改变,这对于商 ...
- Android性能提升之强引用、软引用、弱引用、虚引用使用
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52637333 背景:收到公众投稿 ...
- shell test和find命令实例解析
shell test和find命令实例解析 下面以\build\core\product.mk相关部分来学习 define _find-android-products-files $(shell t ...
- 带你深入理解STL之Stack和Queue
上一篇博客,带你深入理解STL之Deque容器中详细介绍了deque容器的源码实现方式.结合前面介绍的两个容器vector和list,在使用的过程中,我们确实要知道在什么情况下需要选择恰当的容器来满足 ...
- Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)
我们知道,在很多时候,我们都不用Android内置的一些控件,而是自己自定义一些自己想要的控件,这样显得界面更美观. 今天主要是讲自定义AlertDialog和popupWindow的使用,在很多需求 ...
- 如何禁止App在后台运行以及如何保存和恢复App的状态
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果禁止App在后台运行 iOS上的App类似于Windows ...
- 对Bitmap的内存优化
在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitmap时,分给虚拟机中的图片的堆栈大小只有8M,如果超出了,就会出现OutOfMemory异常.所以,对于图 ...
- Android开发学习之路--Notification之初体验
一般当我们收到短信啊,微信啊,或者有些app的提醒,我们都会在通知栏收到一天简单的消息,然后点击消息进入到app里面,其实android中有专门的Notification的类可以完成这个工作,这里就实 ...