ng-option
select 是 AngularJS 预设的一组directive。下面是其官网api doc给出的用法:AngularJS:select
大意是,select中的ngOption可以采用和ngRepeat指令类似的循环结构,其data source可以是array或者是object。针对两种data source又有衍生的好几种用法。但是官网的例子实在是太少了。
下面是针对几个不太容易理解的用法的例子。
先上controller
<!-- lang: js -->
function selectCtrl($scope) {
$scope.selected = '';
$scope.model = [{
id: 10001,
mainCategory: '男',
productName: '水洗T恤',
productColor: '白'
}, {
id: 10002,
mainCategory: '女',
productName: '圆领短袖',
productColor: '黑'
}, {
id: 10003,
mainCategory: '女',
productName: '短袖短袖',
productColor: '黃'
}];
}
实例一:基本下拉效果
usage: label for value in array
<select ng-model="selected" ng-options="m.productName for m in model">
<option value="">-- 请选择 --</option></select>
效果:
.png)
说明
- usage中的 value 也就是 ng-options 中的 m,而 m 是数组model的一个元素,它是一个变量
- usage中的 label 也就是 ng-options 中的
m.productName, 其实就是一个 AngularJS Expression
实例二:自定义下拉显示名称
usage: label for value in array
<select ng-model="selected" ng-options="(m.productColor + ' - ' + m.productName) for m in model">
<option value="">-- 请选择 --</option></select>
效果
.png)
说明
- 可以看到,usage 中的 label 可以根据需求拼接出不同的字符串
实例三: 让选项分组
usage: label group by group for value in array
<select ng-model="selected" ng-options="(m.productColor + ' - ' + m.productName) group by m.mainCategory for m in model">
<option value="">-- 请选择 --</option></select>
效果
.png)
说明
- 这里使用了
group by,通过$scope.model中的mainCategory字段进行分组
实例四:自定义ngModel的值
usage: select as label for value in array
<select ng-model="selected" ng-options="m.id as m.productName for m in model">
<option value="">-- 请选择 --</option></select>
效果
.png)
说明
- 这种用法也是select指令最复杂的一种。从效果中可以看出,usage中select的作用就是重新定义
ng-model的值。在这里,ng-model等于m.id,当ng-model发生改变的时候,得到的是m.id的值
参考
- http://docs.angularjs.org/api/ng.directive:select
- http://blog.miniasp.com/post/2013/05/12/AngularJS-ng-module-select-ngOptions-usage-samples.aspx
一、用法
ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上。
数组:
label for value in array
select as label for value in array
label group by group for value in array
select as label group by group for value in array
select as label group by group for value in array track by trackexpr
对象:
label for ( key , value ) in object
select as label for ( key , value ) in object
label group by group for ( key , value ) in object
select as label group by group for ( key , value ) in object
说明:
二、实例
通用的js代码:
.gif)
<script>
var MyModule = angular.module("MyModule",[]);
MyModule.controller("Ctrl",["$scope", function($scope){
$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light'},
{name:'red', shade:'dark'},
{name:'blue', shade:'dark'},
{name:'yellow', shade:'light'}
];
$scope.object = {
dark: "black",
light: "red",
lai: "red"
};
}]);
</script>
.gif)
label for value in array
html:
<select ng-model="myColor" ng-options="color.name for color in colors"></select>
效果:
.png)

vii5%7Bt.png)
select as label for value in array
html:
<select ng-model="myColor" ng-options="color.shade as color.name for color in colors"></select>
效果:
.png)
label group by group for value in array
html:
<select ng-model="myColor" ng-options="color.name group by color.shade for color in colors"></select>
.png)
select as label group by group for value in array
html:
<select ng-model="myColor" ng-options="color.name as color.name group by color.shade for color in colors">
效果:
.png)

select as label group by group for value in array track by trackexpr
html:
<select ng-model="myColor" ng-options="color.name for color in colors track by color.name">
效果:
.png)

label for ( key , value ) in object
html:
<select ng-model="obj" ng-options="key for (key, value) in object"></select>
效果:
.png)

select as label for ( key , value ) in object
html:
<select ng-model="obj" ng-options="key as key for (key, value) in object"></select>
效果:
.png)

label group by group for ( key , value ) in object
html:
<select ng-model="obj" ng-options="key group by value for (key, value) in object"></select>
.png)

select as label group by group for ( key , value ) in object
html:
<select ng-model="obj" ng-options="key as key group by value for (key, value) in object"></select>
效果:
.png)
ng-option的更多相关文章
- js封装包
(function () { //check the class name , it will be replaced when existed if (window.IQCBase) { //ret ...
- ng 动态的生成option。
ngOptions:根据集合,动态的生成option. select ng-options="color.name for color in colorList" 注意跟ng-re ...
- AngularJS下拉列表select在option动态变化之后多出了一个错误项的问题
场景: Select初始化之后,选中select的某个选项 通过AngularJS更新select的选项 错误写法: HTML(使用ng-repeat) <div ng-app="Te ...
- Flume NG Getting Started(Flume NG 新手入门指南)
Flume NG Getting Started(Flume NG 新手入门指南)翻译 新手入门 Flume NG是什么? 有什么改变? 获得Flume NG 从源码构建 配置 flume-ng全局选 ...
- ng 双向数据绑定 实现 注册协议效果
效果: 代码: <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> ...
- ng 监听数据的变化
$scope.$watch('监听的变量的名称',func) 在angularJs之所以能够实现绑定,是因为angularJS框架在背后为每一个模型数据添加了一个监听,与$watch其实是一个道理. ...
- Windows 环境 ABP前端运行 ng test 无法执行
Command: ng test Error Information: Schema validation failed with the following errors: Data path &q ...
- [C2P2] Andrew Ng - Machine Learning
##Linear Regression with One Variable Linear regression predicts a real-valued output based on an in ...
- 在 C# 里使用 F# 的 option 变量
在使用 C# 与 F# 混合编程的时候(通常是使用 C# 实现 GUI,F#负责数据处理),经常会遇到要判断一个 option 是 None 还是 Some.虽然 Option module 里有 i ...
- jsPanel插件Option总结
jsPanel插件Option总结 学习jsPanel之余对相关的选项进行了总结,便于参考. # 选项名称 类别 简要说明 1 autoclose configuration 设置一个时间在毫秒后,面 ...
随机推荐
- Combination Sum系列问题
主要使用方法是backtracking. Combination Sum Given a set of candidate numbers (C) and a target number (T), f ...
- 【Ubuntu】您没有查看“sf_VirtualDisk”的内容所需的权限。
原文链接:http://www.crifan.com/can_not_access_share_folder_in_ubuntu_virtualbox/ [问题] 之前已经搞定可以自动共享文件夹了: ...
- SVN:重命名文件之后不允许提交
提交文件所属的目录,这样可以提交成功.
- 基于Spring Security 的JSaaS应用的权限管理
1. 概述 权限管理,一般指根据系统设置的安全规则或者安全策略,用户可以访问而且只能访问自己被授权的资源.资源包括访问的页面,访问的数据等,这在传统的应用系统中比较常见.本文介绍的则是基于Saas系统 ...
- BZOJ1857 传送带 (三分法求单峰函数极值)
第一次发BZOJ的题解,先从水题开始吧,好不容易找到一道水题,那就从这题开始吧. 1.题设部分{ 题目描述: 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送带分别为线段AB和线 ...
- 怎样配置mysql_installer_community_V5.6........_setup的数据库?
直接访问查看如下路径就可以根据方法匹配 http://jingyan.baidu.com/album/c35dbcb0f1b1448916fcbcc7.html
- 【Java并发】详解 AbstractQueuedSynchronizer
前言 队列同步器 AbstractQueuedSynchronizer(以下简称 AQS),是用来构建锁或者其他同步组件的基础框架.它使用一个 int 成员变量来表示同步状态,通过 CAS 操作对同步 ...
- Win10隐藏硬盘分区
前几天装了Win10和OpenSUSE的双系统,结果挂载Linux的分区被Windows识别成了三个盘符,在Windows下是不能直接访问它们的,而且双击还会提示你是否格式化. 因为在windows放 ...
- CF CROC 2016 Intellectual Inquiry
题目链接:http://codeforces.com/contest/655/problem/E 大意是Bessie只会英文字母表中的前k种字母,现在有一个长度为m+n的字母序列,Bessie已经知道 ...
- 《Effective C#》读书笔记-1.C# 语言习惯-1.使用属性而不是可访问的数据成员
思维导图: 大纲: 1.使用属性而不是可访问的数据成员 属性 指定不同的访问权限 隐式属性降低了声明属性的工作量 允许将数据成员作为公共接口的一部分暴露 ...