1, index.html:
<! DOCTYPE html>
<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>-->
<body>
<!-- The following is the DEMO2 that the instruction interacts with the controller. -->
<!--the principle of this demo is to assign the method to the property, in JS directly call the property is equal to call the method-->
<div ng-controller= "Myctrl";
<loader loadattr= "LoadData ()" > Sliding load </loader>
</div>
<div ng-controller= " MyCtrl2 "
<loader loadattr=" LoadData2 () "> Sliding load </loader>
</div>
<script src= "./directive-controller2.js" ></SCRIPT>
</body>
2, Directive-controller2.js:
var mymodule = angular.module ("App", []);
Mymodule.controller (' Myctrl ', [' $scope ', function ($scope) {
$scope. Loaddata=function () {
Console.log ("Loading ...");
}
}]);
Mymodule.controller (' MyCtrl2 ', [' $scope ', function ($scope) {
$scope. Loaddata2=function () {
Console.log ("Loading 222222 ...");
}
}]);
Mymodule.directive (' loader ', function () {
return {
Restrict: ' AE ',
Link:function (scope,element,attrs) {
Element.bind (' MouseEnter ', function (event) {
Scope. $apply (attrs.loadattr);//The invocation of a method in a property does not have to be written like this, note that the property call must be lowercase, or invalid, regardless of whether the attribute in the HTML is uppercase or lowercase
})
}
}
});
[email protected] the more complex interaction between the instruction and the controller Demo2