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. Dynamics CRM2016 在实体命名时需要注意的事项

    在使用web api的过程中遇到个很无语的设置,体现在对实体名的设置上,之前看到accounts以为只是在实体名上加个s,也没往深处看,但真正进入项目实施了问题就来了,city直接变成了cities不 ...

  2. android 自定义ViewGroup之浪漫求婚

    *本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 1.最终效果 有木有发现还是很小清新的感觉 2.看整体效果这是一个scrollView,滑动时每个子view都有一个或多个动画效果 ...

  3. TextView的升级版———AutoCompleteTextView

    TextView的升级版---AutoCompleteTextView AutoCompleteTextView顾名知义,可以自动提示的TextView,还可以提示错误信息. 这里介绍基本的使用,能够 ...

  4. Scikit-learn:数据预处理Preprocessing data

    http://blog.csdn.net/pipisorry/article/details/52247679 本blog内容有标准化.数据最大最小缩放处理.正则化.特征二值化和数据缺失值处理. 基础 ...

  5. Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》

    昨晚写博客太仓促,代码结构有问题,早上测试发现没法监听文本变化!今日更改一下.真心见谅啦,哈哈!主活动的代码已经改好了,看截图这次的确实现了文本监听变化情况. 监听文本输入情况,仅仅限于土司略显low ...

  6. 安卓TextView完美展示html格式代码

    对于TextView展示html格式代码,最简单的办法就是使用textview.setText(Html.fromHtml(html));,即便其中有img标签,我们依然可以使用ImageGetter ...

  7. linux中的网络通信指令

    1.write write命令通信是一对一的通信,即两个人之间的通信,如上图. 效果图 用法:write <用户名> 2.wall wall指令可将信息发送给每位同意接收公众信息的终端机用 ...

  8. SSH深度历险(九) Struts2+DWZ+Uploadify实现多文件(文件和图片等等)上传

    在gxpt_uas系统中,要实现文件(文件和图片等等,可以灵活配置)的批量上传至mongodb,在学习这个过程中,学习了mongodb,并实现了批量上传的功能,实现思路:在DWZ的基础上参考官方的实例 ...

  9. Dynamics CRM 2013 SP1 客户表单界面上联系人subgrid上的添加现有联系人功能缺失

    CRM2013打了SP1的同学会发现一个问题,客户关联联系人的1:N关系,在表单subgrid中添加联系人时,只能新建而无法添加现有联系人,而这个现象在之前的版本中是没有的. 我们通过工具ribbon ...

  10. Aandroid TV 基于Leanback支持最新MD设计的TV开发框架

    原文地址:http://blog.csdn.net/sk719887916 作者:skay 基于6.0最新的API 支持TV的框架 Android 6.0已完美支持TV开发,之前的5.0后Recycl ...