<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>