1 <body ng-app="app" ng-controller="controller"> 2 <drop-down-list d-list="testList" value="id" text="text" d-select-value="value" ></drop-down-list> 3 {{value}} 4 <script src="/bootstrap/bootstrap/dist/angular-bootstrap/angular.js?1.1.11"></script> 5 <script type="text/javascript"> 6 var app= angular.module("app",[]); 7 app.controller("controller",["$scope","$window",function ($scope,$window) { 8 var testList=[{id:0,text:" 全国"},{id:1,text:" 北京"},{id:20,text:" 上海"},{id:3,text:" 福建"},{id:4,text:" 山东"}]; 9 $scope.value=20;10 $scope.testList=testList;11 }]);12 app.directive("dropDownList",function () {13 return{14 restrict:'E',15 scope :{16 dList:'=',17 dSelectValue:'='18 } ,19 link:function(scope, element, attrs) {20 var d=document;21 var value=attrs["value"];//对应option的value22 var text=attrs["text"];23 var selectValue=scope.dSelectValue;24 element.on("change",function(){25 var selectedIndex=this.selectedIndex;26 scope.$apply(function(){27 scope.dSelectValue=selectedIndex;28 });29 })30 31 for(var i=0;i<scope.dList.length;i++)32 {33 var option=d.createElement("option");34 option.value=scope.dList[i][value];35 option.innerHTML=scope.dList[i][text];36 if(selectValue==option.value)37 {38 option.setAttribute("selected",true);39 }40 element.append(option);41 }42 },43 template:'<select></select>',44 replace:true45 46 };47 });48 49 </script>50 </body>