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 效果 http://sandbox.runjs.cn/show/nhi8ubrb
<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
以上所述就是本文的全部內容了,希望大家能夠喜歡。