AngularJS学习笔记之ng-options指令
1.基本下拉效果(lableforvalueinarray)
其中select标签中的ng-model属性必须有,其值为选中的对象或属性值。
<divng-controller="ngselect"> <p>usage:labelforvalueinarray</p> <p>选项,{{selected}}</p> <selectng-model="selected"ng-options="o.idforoinoptData"> <optionvalue="">--请选择--</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.自定义下拉显示名称(labelforvalueinarray)
label可以根据需要拼接出不同的字符串
<divng-controller="ngselect2"> <p>usage:labelforvalueinarray(label可以根据需求拼接出不同的字符串)</p> <p>选项,{{selected}}</p> <selectng-model="selected"ng-options="(o.ProductColor+'-'+o.ProductName)foroinoptData"> <optionvalue="">--请选择--</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选项分组
groupby分组项
<divng-controller="ngselect3"> <p>usage:labelgroupbygroupNameforvalueinarray</p> <p>选项,{{selected}}</p> <selectng-model="selected"ng-options="(o.ProductColor+'-'+o.ProductName)groupbyo.MainCategoryforoinoptData"> <optionvalue="">--请选择--</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
<divng-controller="ngselect4"> <p>usage:selectaslabelforvalueinarray</p> <p>选项,{{selected}}</p> <selectng-model="selected"ng-options="o.idaso.ProductNameforoinoptData"> <optionvalue="">--请选择--</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
以上所述就是本文的全部内容了,希望大家能够喜欢。