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= ...
随机推荐
- Programming In Scala笔记-第二、三章
本系列博客以<Programming in Scala 2nd Edition>为主,围绕其中的代码片段进行学习和分析. 本文主要梳理Chapter2和Chapter3中涉及到的主要概念. ...
- Java异常处理-----非运行时异常(受检异常)
非运行时异常(受检异常) 如果出现了非运行时异常必须进行处理throw或者try{}catch(){}处理,否则编译器报错. 1:IOException 使用要导入包import java.io.IO ...
- C++ 虚函数表 单继承
本文研究单继承情况下,c++对象的虚函数表的具体情况. 假设有两个类A,B, 其中B由A派生出来,A含有虚函数fun1,B含有虚函数fun2. 测试的代码如下: #include<iostrea ...
- [error]configure: error: You need a C++ compiler for C++ support.
安装pcre包的时候提示缺少c++编译器 解决办法 使用yum安装 yum -y install gcc-c++ 本文出自 "orangleliu笔记本"博客,转载请务必保留此出处 ...
- webstorm工具使用详解
webstorm简单介绍 官网地址:http://www.jetbrains.com/webstorm/features/index.html 参考地址:http://www.html5jscss.c ...
- Python optparser库详解
一直以来对optparser不是特别的理解,今天就狠下心,静下心研究了一下这个库.当然了,不敢说理解的很到位,但是足以应付正常的使用了.废话不多说,开始今天的分享吧. 简介 optparse模块主要用 ...
- EBS总账模块与其他模块数据关联关系
表名:GL_IMPORT_REFERENCES 说明:总账导入附加信息表 用途:用来追溯从子模块传入总账模块的明细,对于报表开发很有帮助 SQL 语句: select * from gl_je_hea ...
- Android支持多国语言化Values命名
android多国语言文件夹文件汇总如下: 维吾尔文(中国):values-ug-rCN 中文(中国):values-zh-rCN 中文(台湾):values-zh-rTW 中文(香港):values ...
- hive编程指南——读书笔记(无知拾遗)
set hive.metastore.warehouse.dir=/user/myname/hive/warehouse; 用户设定自己的数据仓库目录.不影响其他用户.也在$HOME/.hiverc中 ...
- Unity插件 - MeshEditor(四) 模型融化特效
现在的电影里有很多妖魔在死亡后身体逐渐融化并下滑最后化为一滩黑水的情景,本次出于兴趣大致研究了这个效果,原理是控制模型的顶点向一个方向坍塌,坍塌到最低点时再根据法线方向扩散形成黑水状. 第一步: 添加 ...