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>
<body>
<!--The following is a demo of the instructions interacting with the controller. -
<div ng-controller= "Myctrl" >
<loader> Sliding Loading </loader>
</div>
<script src= "./directive-controller.js" ></script>
</body>
2, Directive-controller.js:
var mymodule = angular.module ("App", []);
Mymodule.controller (' Myctrl ', [' $scope ', function ($scope) {
$scope. Loaddata=function () {
Console.log ("Loading ...");
}
}]);
Mymodule.directive (' loader ', function () {
return {
Restrict: ' AE ',
Replace:true,
transclude:true,//This is also a pit, declaring the above substitution and this nested property without declaring the template substitution and nesting, the light in the HTML to write a custom directive label will not come out of anything
scope:{},//this is a pit. When the controller interacts with the instruction cannot declare the independent scope, otherwise the scope becomes private, the instruction will not see the method defined in the controller, the program error: said that the private scope cannot find the LoadData method
Link:function (scope,element,attrs) {
Element.bind (' MouseEnter ', function (event) {
Scope. $apply ("LoadData ()");
Scope.loaddata ();
})
}
}
});
[email protected] interaction between command and Controller demo