ng-option 有数组,对象两种情况,但目前只接触过数组

数组:

label for value in array

分为一般数组和对象数组

一般数组:

<select ng-model="myAddress" ng-options="item for item in address"></select>

$scope.address= ["北京", "天津", "河北"];

发现第一项为空

dom树如下:

解析: 1.当未设置初始值时,初始值及option列表第一项为空,默认第一项被选 2.value等于label

设置初始值方法:

1.$scope.myAddress= $scope.address[0];

注: 初始值必须得是数组中的某一项,否则无效

则变为:

2.新增option

<select ng-model="myAddress" ng-options="item for item in address">
  <option value="" disabled="">请选择地址</option>
</select>

(值得一提的是这种方法只能新增一个option,多写的不会出现)

变为:

disable的option会变为不可选的灰色,此种方法应用面更广

对象数组:

<select ng-model="myColor1" ng-options="color.name for color in colors"></select>

$scope.colors = [
  {name:'black', shade:'dark'},
  {name:'white', shade:'light', notAnOption: true},
  {name:'red', shade:'dark'},
  {name:'blue', shade:'dark', notAnOption: true},
  {name:'yellow', shade:'light', notAnOption: false}];

dom树如下:

解析: 写法与一般数组不同,value为label所属的对象

---------------------------------------------------------------------------------------------------------------------------------------------------------

label group by group for value in array(选项分组)

<select ng-model="myColor2" ng-options="color.name group by color.shade for color in colors"></select>

                           label                    分组依据属性

$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light', notAnOption: true},
{name:'red', shade:'dark'},
{name:'blue', shade:'dark', notAnOption: true},
{name:'yellow', shade:'light', notAnOption: false}
];

可见按照shade属性进行了分组

----------------------------------------------------------------------------------------------------------------------------------------------------------

label disable when disable for value in array(将部分option设为disable)

<select ng-model="myColor3" ng-options="color.name disable when color.notAnOption for color in colors"></select>

                        label                          disable依据属性

$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light', notAnOption: true},
{name:'red', shade:'dark'},
{name:'blue', shade:'dark', notAnOption: true},
{name:'yellow', shade:'light', notAnOption: false}
];

可以看出当notAnOption未false时不可点击

--------------------------------------------------------------------------------------------------------------------------------------------

select as label for value in array(设置value)

<select ng-model="myColor4" ng-options="color.shade as color.name for color in colors"></select>

                       value             label

可见shade成了value,name成了label

-------------------------------------------------------------------------------------------------------------------------------------------------

ng-option小解的更多相关文章

  1. js封装包

    (function () { //check the class name , it will be replaced when existed if (window.IQCBase) { //ret ...

  2. ng 动态的生成option。

    ngOptions:根据集合,动态的生成option. select ng-options="color.name for color in colorList" 注意跟ng-re ...

  3. AngularJS下拉列表select在option动态变化之后多出了一个错误项的问题

    场景: Select初始化之后,选中select的某个选项 通过AngularJS更新select的选项 错误写法: HTML(使用ng-repeat) <div ng-app="Te ...

  4. Flume NG Getting Started(Flume NG 新手入门指南)

    Flume NG Getting Started(Flume NG 新手入门指南)翻译 新手入门 Flume NG是什么? 有什么改变? 获得Flume NG 从源码构建 配置 flume-ng全局选 ...

  5. ng 双向数据绑定 实现 注册协议效果

    效果: 代码: <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> ...

  6. ng 监听数据的变化

    $scope.$watch('监听的变量的名称',func) 在angularJs之所以能够实现绑定,是因为angularJS框架在背后为每一个模型数据添加了一个监听,与$watch其实是一个道理. ...

  7. Windows 环境 ABP前端运行 ng test 无法执行

    Command: ng test Error Information: Schema validation failed with the following errors: Data path &q ...

  8. [C2P2] Andrew Ng - Machine Learning

    ##Linear Regression with One Variable Linear regression predicts a real-valued output based on an in ...

  9. 在 C# 里使用 F# 的 option 变量

    在使用 C# 与 F# 混合编程的时候(通常是使用 C# 实现 GUI,F#负责数据处理),经常会遇到要判断一个 option 是 None 还是 Some.虽然 Option module 里有 i ...

  10. jsPanel插件Option总结

    jsPanel插件Option总结 学习jsPanel之余对相关的选项进行了总结,便于参考. # 选项名称 类别 简要说明 1 autoclose configuration 设置一个时间在毫秒后,面 ...

随机推荐

  1. C++ 内存分析-valgrind

    valgrind包括了以下几个比较重要的模块:memcheck, cachegrind, callgrind, helgrind, drd, massif, dhat, sgcheck, bbv. 还 ...

  2. 更好列表页中一个航班.先unset删除数组中一个键值对,再追加,最后按键排序

    <?php $arr = array( '0' => array('item' => array( 'aa' => 'aaa', 'bb' => 'bbb' )), '1 ...

  3. 2016NEFU集训第n+3场 G - Tanya and Toys

    Description In Berland recently a new collection of toys went on sale. This collection consists of 1 ...

  4. Java 内部类和匿名内部类

    1.内部类: /* 局部内部类: 在一个类 的方法内部定义另外一个类,那么另外一个类就称作为局部内部类. 局部内部类要注意的细节: 1. 如果局部 内部类访问了一个局部变量,那么该局部变量必须使用fi ...

  5. ios 将彩色照片转化成黑白等几种类型

    -(UIImage *)changeColoursImageTograyScaleImage:(UIImage *)anImage type:(int)type { CGImageRef imageR ...

  6. Round Numbers(组合数学)

    Round Numbers Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

  7. javacript没有多维数组只能模拟?

    为什么说javacript没有多维数组只能模拟?但是我看到是可以实现多维数组的啊!这是为什么呢?下面我们来实验下. <!DOCTYPE html> <html lang=" ...

  8. Python 学习笔记12

    不积跬步,无以至千里.不积小流,无以成江河. 当我觉得沮丧.绝望的时候,就疯狂的敲代码,这样会好受一点. 今天和昨天敲了两天的小程序,算是对python的具体语法规则有个初步的手熟. http://w ...

  9. Find and run the whalesay image

    Find and run the whalesay image People all over the world create Docker images. You can find these i ...

  10. perl版 Webshell存活检测

    原理: 检测url返回状态即可 源码: #!c:\\perl\\bin\\perl.exe use warnings; use strict; use LWP::UserAgent; $| = ; p ...