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. win10应用UserControl

    <Grid xmlns:src="using:UserControlExample" Margin="0,50,0,0"> <Grid.Row ...

  2. Spring Security(10)——退出登录logout

    要实现退出登录的功能我们需要在http元素下定义logout元素,这样Spring Security将自动为我们添加用于处理退出登录的过滤器LogoutFilter到FilterChain.当我们指定 ...

  3. Integer.valueOf(int)及自动装箱内幕

    Integer为什么要提供功能与new Integer(xx)一样的valueOf(xx)方法呢,看了源代码之后,我发现了惊人的内幕. public static Integer valueOf(in ...

  4. 再次深入 C# Attribute

    了解attribute Attribute 只是将一些附加信息与某个目标元素关联起来的方式. Attribute 是一个类,这个类可以提供一些字段和属性,不应提供公共方法,事件等.在定义attribu ...

  5. IO.Path路径

    string filePath =@"E:/Randy0528/中文目录/JustTest.rar"; 更改路径字符串的扩展名.System.IO.Path.ChangeExten ...

  6. 【Loadrunner】初学Loadrunner——IP欺骗

    因为在默认情况下,同一个用户用同一个IP访问运行是不符合实际情况的,而且很多网站会自动屏蔽同个IP多次重复访问.那么就想到了Loadrunner的虚拟IP技术,也就是常说的IP欺骗.在用Loadrun ...

  7. 实验六 多线程编程 1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

    //继承Thread类 package zuoye; //继承Thread类 public class City extends Thread{ private String name; public ...

  8. 【开发笔记】Spring的配置文件

    factory-method竟然写成了destroy-method,害得我运行报错,找原因找了几分钟. 原来我按alt+/来提示代码的时候,有许多可选项,可能一时眼花选错了

  9. 基于Flash与window平台本地程序通信实现媒体流发布

    0 Web场景下的媒体流发布可以采用Flash原生API实现,但是Flash H264视频压缩参数不可控.音频无法AAC编码,所以一般采用浏览器插件方式,但是浏览器插件有版本兼容问题.不稳定,所以可以 ...

  10. Ubuntu编译安装PHP7

    参数配置 ./configure --prefix=/usr/local/php7 \ --with-config-file-path=/usr/local/php7/etc \ --with-apx ...