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

1.基本下拉效果(lable for value in array)

  其中select标签中的ng-model属性必须有,其值为选中的对象或属性值。

<div ng-controller="ngselect">
<p>usage:label for value in array</p>
<p>选项,{{selected}}</p>
<select ng-model="selected" ng-options="o.id for o in optData">
<option value="">-- 请选择 --</option>
</select>
</div>
m1.controller("ngselect",['$scope',function($sc){
$sc.selected = '';
$sc.optData = [{
id: 10001,
MainCategory: '男',
ProductName: '水洗T恤',
ProductColor: '白'
},{
id: 10002,
MainCategory: '女',
ProductName: '圓領短袖',
ProductColor: '黃'
},{
id: 10003,
MainCategory: '女',
ProductName: '圓領短袖',
ProductColor: '黃'
}];
}]);

2.自定义下拉显示名称(label for value in array)

    label可以根据需要拼接出不同的字符串
<div ng-controller="ngselect2">
<p>usage:label for value in array(label可以根据需求拼接出不同的字符串)</p>
<p>选项,{{selected}}</p>
<select ng-model="selected" ng-options="(o.ProductColor+'-'+o.ProductName) for o in optData">
<option value="">-- 请选择 --</option>
</select>
</div>
m1.controller("ngselect2",['$scope',function($sc){
$sc.selected = '';
$sc.optData = [{
id: 10001,
MainCategory: '男',
ProductName: '水洗T恤',
ProductColor: '白'
},{
id: 10002,
MainCategory: '女',
ProductName: '圓領短袖',
ProductColor: '黃'
},{
id: 10003,
MainCategory: '女',
ProductName: '圓領短袖',
ProductColor: '黃'
}];
}]);

3.ng-options 选项分组

    group by分组项
<div ng-controller="ngselect3">
<p>usage:label group by groupName for value in array</p>
<p>选项,{{selected}}</p>
<select ng-model="selected" ng-options="(o.ProductColor+'-'+o.ProductName) group by o.MainCategory for o in optData">
<option value="">-- 请选择 --</option>
</select>
</div>
m1.controller("ngselect3",['$scope',function($sc){
$sc.selected = '';
$sc.optData = [{
id: 10001,
MainCategory: '男',
ProductName: '水洗T恤',
ProductColor: '白'
},{
id: 10002,
MainCategory: '女',
ProductName: '圓領长袖',
ProductColor: '黃'
},{
id: 10003,
MainCategory: '女',
ProductName: '圓領短袖',
ProductColor: '黃'
}];
}]);

4.ng-options 自定义ngModel的绑定

    下面selected的值为optData的id
<div ng-controller="ngselect4">
<p>usage:select as label for value in array</p>
<p>选项,{{selected}}</p>
<select ng-model="selected" ng-options="o.id as o.ProductName for o in optData">
<option value="">-- 请选择 --</option>
</select>
</div>
m1.controller("ngselect4",['$scope',function($sc){
$sc.selected = '';
$sc.optData = [{
id: 10001,
MainCategory: '男',
ProductName: '水洗T恤',
ProductColor: '白'
},{
id: 10002,
MainCategory: '女',
ProductName: '圓領长袖',
ProductColor: '黃'
},{
id: 10003,
MainCategory: '女',
ProductName: '圓領短袖',
ProductColor: '黃'
}];
}]);

5.ng-options 多级下拉

<div ng-controller="ngselect5">
<select ng-model="selectedPerson" ng-options="obj.name for obj in people"></select>
<select ng-model="selectedGenre">
<option ng-repeat="label in people[selectedPerson.id].interest">{{label}}</option>
</select>
</div>
m1.controller("ngselect5",['$scope',function($sc){
$sc.people = [
{
id: 0,
name: '张三',
interest: [
'爬山',
'游泳',
'旅游',
'美食'
]
},
{
id: 1,
name: '李四',
interest: [
'音乐',
'美食',
'Coffee',
'看书'
]
},
{
id: 2,
name: '王五',
interest: [
'音乐',
'电影',
'中国好声音',
'爸爸去哪了',
'非常静距离'
]
},
{
id: 3,
name: '小白',
interest: [
'游泳',
'游戏',
'宅家里'
]
}
];
}]);

原网址:http://www.cnblogs.com/zhx1991/p/4579886.html

最新文章

  1. [UWP]创建自定义VisualState Trigger
  2. 到处都是坑的微信支付V3之 微信支付回调页面
  3. SQL查询数据的几大方法
  4. 【poj1144】 Network
  5. javaWeb el表达式和jstl快速入门案例
  6. Java线程练习
  7. Spring MVC 注解和XML的区别
  8. pyqt 动态显示时间方法例子学习
  9. struts2 &lt;s:iterator&gt; 遍历方法
  10. C++中的操作符重载
  11. storm-编程入门
  12. CSS3的box-sizing属性
  13. WPF介绍
  14. echarts地图详解
  15. 用主题模型可视化分析911新闻(Python版)
  16. Codeforces.1051F.The Shortest Statement(最短路Dijkstra)
  17. localStorage 设置本地缓存
  18. Alpha冲刺3
  19. 【C++】子类访问父类typedef的问题
  20. stm32的双向io口

热门文章

  1. laravel 中禁用掉注册功能
  2. cocos2d-x 3.17 jsb android运行报错
  3. RankNet
  4. ubuntu 添加应用到Dash启动器
  5. Ubuntu12.04中Gvim无法固定到启动器的解决办法
  6. 实现一个 WPF 版本的 ConnectedAnimation
  7. 《DSP using MATLAB》示例Example 8.11
  8. flask第十八篇——模板【2】
  9. 获得消息的x,y的窗口内坐标(包括边框和titlebar高度
  10. CH1801 括号画家