<html> <head> <meta charset="utf-8"/> <title></title> </head> <body ng-app="components"> <div ng-controller="c1"> <myelement alert="loadData1()"> <h1>哈哈</h1> </myelement> </div> <div ng-controller="c2"> <myelement alert="loadData2()"> <h1>吼吼</h1> </myelement> </div> </body> <script src="angular.js"></script> <script> var app= angular.module('components', []); app.controller("c1",function($scope){ $scope.loadData1=function(){ alert("哈哈"); } }); app.controller("c2",function($scope){ $scope.loadData2=function(){ alert("吼吼"); } }); app.directive("myelement",function(){ return { link:function(scope, element, attr, superCtrl){ element.bind("click",function(){ scope.$apply(attr.alert);//在這里注意下如果自定義屬性使用駝峰命名法,那么attr調用該屬性時仍然要保證完全小寫 }); } } }); </script> </html>