AngularJS学习笔记之ng-options指令
脚本之家 / 编程助手:解决程序员“几乎”所有问题!
脚本之家官方知识库 → 点击立即使用
1.基本下拉效果(lable for value in array)
其中select标签中的ng-model属性必须有,其值为选中的对象或属性值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <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可以根据需要拼接出不同的字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <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分组项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <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 效果 http://sandbox.runjs.cn/show/nhi8ubrb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <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: '黃' }]; }]); |
效果:http://runjs.cn/detail/nhi8ubrb
以上所述就是本文的全部内容了,希望大家能够喜欢。

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
相关文章
使用typescript开发angular模块并发布npm包
本篇文章主要介绍了使用typescript开发angular模块并发布npm包,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-04-04angularJS利用ng-repeat遍历二维数组的实例代码
本篇文章主要介绍了angularJS利用ng-repeat遍历二维数组的实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-06-06详解AngularJS用Interceptors来统一处理HTTP请求和响应
本篇文章主要介绍了AngularJS用Interceptors来统一处理HTTP请求和响应 ,具有一定的参考价值,有兴趣的可以了解一下2017-06-06AngularJS2 与 D3.js集成实现自定义可视化的方法
本篇文章主要介绍了ANGULAR2 与 D3.js集成实现自定义可视化的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12解决Angular.js中使用Swiper插件不能滑动的问题
下面小编就为大家分享一篇解决Angular.js中使用Swiper插件不能滑动的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-02-02
最新评论