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实现下拉菜单单选的更多相关文章

  1. 纯css和js版下拉菜单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. js 联动下拉菜单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. js版本下拉菜单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. JS实现下拉菜单的功能

    <!DOCTYPE html> <html> <head> <meta charset = "utf8"> <title> ...

  5. js获取下拉,单选

    1.JS取下拉框的显示值和判断单选按钮 1.需要得到select组件显示的值.下面是经常用到的方法: Html 源码: <html><body><select id=&q ...

  6. 第二百四十四节,Bootstrap下拉菜单和滚动监听插件

    Bootstrap下拉菜单和滚动监听插件 学习要点: 1.下拉菜单 2.滚动监听 本节课我们主要学习一下 Bootstrap 中的下拉菜单插件,这个插件在以组件的形式我们 已经学习过,那么现在来看看怎 ...

  7. 关于Eclipse插件开发(四)-------给视图加下拉菜单和按钮和加入编辑器.

    本例将给视图加入下拉菜单和按钮,同时再为列表添加一个右键菜单. 创建ActionGroup类 加入菜单和按钮的方法与SWT和JFace组件的一样,先创建一个ActionGroup代码如下: MyAct ...

  8. js模拟下拉菜单-键盘、鼠标(代码详解)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. bootstrap和JS实现下拉菜单

    // bootstrap下拉菜单 <div class="btn-group"> <button id="button_text" type= ...

随机推荐

  1. Weblogic 12c 负载均衡和session复制

    在上一篇,我们介绍了weblogic集群的部署和session的复制,如何将请求负载均衡到这个三个服务器上呢? 这里提供两种方式:(1)weblogic自带的proxy代理        (2) ng ...

  2. Android简易实战教程--第四十三话《上拉加载与下拉刷新》

    ListView的下拉刷新很常见,很多开源的框架都能做到这个效果,当然也可以自己去实现.本篇案例是基于xlistview的. 布局: <RelativeLayout xmlns:android= ...

  3. java自动装箱拆箱总结

    对于java1.5引入的自动装箱拆箱,之前只是知道一点点,最近在看一篇博客时发现自己对自动装箱拆箱这个特性了解的太少了,所以今天研究了下这个特性.以下是结合测试代码进行的总结. 测试代码: int a ...

  4. String放入运行时常量池的时机与String.intern()方法解惑

    运行时常量池概述 Java运行时常量池中主要存放两大类常量:字面量和符号引用.字面量比较接近于Java语言层面的常量概念,如文本字符串.声明为final的常量值等. 而符号引用则属于编译原理方面的概念 ...

  5. SQL LOADER使用

    转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17048907 1.执行的命令 sqlldr 数据库用户名/密码 control=控制文 ...

  6. Android逆向工程

    在Root前提下,我们可以使用Hooker方式绑定so库,通过逆向方式篡改数值,从而达到所谓破解目的.然而,目前无论是软件加固方式,或是数据处理能力后台化,还是客户端数据真实性验证,都有了一定积累和发 ...

  7. Shell脚本生成网页版相册浏览器

    今天学到了一招,那就是使用脚本制作一款网页版相册浏览器.先上图吧. 必备基础 操作系统: 以linux为内核的操作系统都行 编程语言:Shell(bash)脚本,相关基础知识即可 下载工具:wget ...

  8. UNIX网络编程——非阻塞connect

    当在一个非阻塞的TCP套接字上调用connect时,connect将立即返回一个EINPROGRESS错误,不过已经发起的TCP三次握手继续进行.我们接着使用select检测这个连接或成功或失败的已建 ...

  9. Android优化之ArrayMap

    ArrayMap的介绍 官方对ArrayMap也有说明:它不是一个适应大数据的数据结构,相比传统的HashMap速度要慢,因为查找方法是二分法,并且当你删除或者添加数据时,会对空间重新调整,在使用大量 ...

  10. iOS中 动态热修补技术JSPatch 韩俊强的博客

    .1.4) JSPatch bridge Objective-C and JavaScript. You can call any Objective-C class and method in Ja ...