一个下拉菜单的实例
<!DOCTYPE html> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>RunJS</title> <style> button{ border:1px solid #ccc; cursor:pointer; display:block; margin:auto; position:relative; top:100px; } </style> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <h3>ng-options 基本下拉效果</h3> <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> <h3>ng-options 自定义下拉显示名称</h3> <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> <h3>ng-options 选项分组</h3> <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> <h3>ng-options 自定义ngModel的绑定</h3> <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> <h3>ng-options多级下拉</h3> <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> <script> var m1 = angular.module('myApp',[]); 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: '黃' }]; }]); 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: '黃' }]; }]); 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: '黃' }]; }]); 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: '黃' }]; }]); 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: [ '游泳', '游戏', '宅家里' ] } ]; }]); </script> </body> </html>