1 <html> 2 <head> 3 <meta charset="utf-8"/> 4 <title></title> 5 </head> 6 <body ng-app="components"> 7 <myelement> 8 <h1>哈哈</h1> 9 </myelement> 10 </body> 11 <script src="angular.js"></script> 12 <script> 13 var app= angular.module('components', []); 14 app.directive('myelement', function() { 15 return { 16 restrict: 'AE',//自定义属性和Dom节点 17 transclude: true,//将自定义指令内部原有的节点保留(这里注意,自定义指令中的内容在模板中必须有父节点) 18 scope: {}, 19 templateUrl:"templates/templateOther.html", 20 replace: true,//全部替换 21 link:function(scope,element,attr){ 22 element.bind("click",function(){ 23 alert(1); 24 }) 25 } 26 }; 27 }); 28 </script> 29 </html>