1、index.html:
<!DOCTYPE HTML>
<html ng-app="app">
<head>
<title>custom-directive</title>
<meta charset="utf-8">
<link rel="stylesheet" href="../css/bootstrap.css">
<script src="../js/angular.js"></script>
<!--<script src="../js/jquery-1.10.2.min.js.js"></script>-->
</head>
<body>
<!-- 下面是指令与控制器交互的demo2. -->
<!-- 这个demo的原理是将方法赋给属性,在js中直接调用属性就等于直接调用方法 -->
<div ng-controller="myCtrl">
<loader loadAttr="loadData()">滑动加载</loader>
</div>
<div ng-controller="myCtrl2">
<loader loadAttr="loadData2()">滑动加载</loader>
</div>
<script src="./directive-controller2.js"></script>
</body>
</html>
2、directive-controller2.js:
var myModule = angular.module("app",[]);
myModule.controller('myCtrl', ['$scope', function($scope){
$scope.loadData=function(){
console.log("正在加载......");
}
}]);
myModule.controller('myCtrl2', ['$scope', function($scope){
$scope.loadData2=function(){
console.log("正在加载222222......");
}
}]);
myModule.directive('loader',function(){
return {
restrict:'AE',
link:function(scope,element,attrs){
element.bind('mouseenter',function(event){
scope.$apply(attrs.loadattr);//属性中的方法的调用非要这样写,注意:不管html中的属性是大写还是小写,这里的属性调用都必须是小写,否则无效
})
}
}
});