ng-option指令使用记录,设置默认值需要注意
2016-11-21 19:33
531 查看
ng-options一般有以下用法:
数组作为数据源:
label for value in array
select as label for value in array
label group by group for value in array
label disable when disable for value in array
label group by group for value in array track by trackexpr
label disable when disable for value in array track by trackexpr
label for value in array | orderBy:orderexpr track by trackexpr(for including a filter with track by)
对象作为数据源:
label for (key , value) in object
select as label for (key ,value) in object
label group by group for (key,value) in object
label disable when disable for (key, value) in object
select as label group by group for(key, value) in object
select as label disable when disable for (key, value) in object
使用事例如下:select element
这是ng-options表达式的基本形式,形如"<标签>" for <项目> in <数组or对象>这样的形式,angularjs会为数组中的每一个对象生成一个option元素,并且将其值设置到标签中去。
选择一个列表时ng-model的值会指向select元素的当前选中项的value值.
对于这个select元素会生成如下的HTML:
View Code
总结
接触一个指令学习一个指令,如何设置默认值需要注意,顺手总结在这里,也是一种学习。
数组作为数据源:
label for value in array
select as label for value in array
label group by group for value in array
label disable when disable for value in array
label group by group for value in array track by trackexpr
label disable when disable for value in array track by trackexpr
label for value in array | orderBy:orderexpr track by trackexpr(for including a filter with track by)
对象作为数据源:
label for (key , value) in object
select as label for (key ,value) in object
label group by group for (key,value) in object
label disable when disable for (key, value) in object
select as label group by group for(key, value) in object
select as label disable when disable for (key, value) in object
使用事例如下:select element
<select ng-model="myColor1" ng-options="color.name as color.name for color in colors"></select>
这是ng-options表达式的基本形式,形如"<标签>" for <项目> in <数组or对象>这样的形式,angularjs会为数组中的每一个对象生成一个option元素,并且将其值设置到标签中去。
选择一个列表时ng-model的值会指向select元素的当前选中项的value值.
对于这个select元素会生成如下的HTML:
<select ng-model="myColor1" ng-options="color.name as color.name for color in colors" class="ng-valid ng-dirty ng-valid-parse ng-touched"> <option value="0" label="black">black</option> <option value="1" label="white">white</option> <option value="2" label="red">red</option> <option value="3" label="blue">blue</option> <option value="4" selected="selected" label="yellow">yellow</option> </select>
<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <meta charset="UTF-8"> <title>Title</title> <style> select{width: 80px;} </style> </head> <body ng-controller="myCtrl"> <select ng-model="val" ng-options="item.label for item in arr | orderBy:'id' track by item.id"></select> </body> <script src="angular.js"></script> <script> angular.module("myApp",[]) .controller("myCtrl",function($scope){ $scope.arr=[ {id:4,label:31}, {id:3,label:22}, {id:1,label:11}, {id:5,label:41}, {id:2,label:21} ]; $scope.val = $scope.arr[0];//设置默认值 }) </script> </html>
View Code
总结
接触一个指令学习一个指令,如何设置默认值需要注意,顺手总结在这里,也是一种学习。
相关文章推荐
- 使用jquery的ajax需要注意的地方dataType的设置
- 使用layoutParams设置控件高宽需要注意的问题
- 使用jquery的ajax需要注意的地方dataType的设置
- 使用layoutParams设置控件高宽需要注意的问题
- 学习记录-使用QTabWidget在析构时需要注意的问题
- 使用angularjs的ng-options时如何设置默认值(初始值)
- Flex中使用全屏设置和需要注意的地方
- 使用angularjs的ng-options时如何设置默认值(初始值)
- 友盟第三方QQ登录,报错注意,使用友盟社会化组件的微博SSO功能,需要按照文档要求在xcode设置url scheme!
- 使用VC 编程改变字体 注意点--需要设置GB2312—CHARSET
- Xcode项目使用SVN需要注意的问题及设置
- angularjs中使用ng-repeat需要注意的东西
- 使用jquery的ajax需要注意的地方dataType的设置 (目前我也不是很清楚哎)
- 使用com.jayway.jsonpath.JsonPath包进行JSON的快速解析、设置值需要注意的性能提升方法
- 详解使用angularjs的ng-options时如何设置默认值(初始值)
- 使用DELETE ADJACENT DUPLICATES FROM 删除内表邻近重复记录需要注意事项
- 记录下串口使用需要注意的地方
- 注意,使用友盟社会化组件的微博SSO功能,需要按照文档要求在xcode设置url scheme!
- Xcode项目使用SVN需要注意的问题及设置
- Docker学习系列(三):Ubuntu下使用Docker的基本指令记录及一些注意事项