Directive:html
//Element directive app.directive('superman', function(){ return { restrict: "E", template: "<div>Here I am to save the world!</div>", link: function(){ alert("Superman!"); } } });
<superman></superman>
app.directive('superman', function(){ return { restrict: "A", template: "<div>Here I am to save the world!</div>", link: function(){ alert("Superman!"); } } });
<div superman></div>
Directive also have 'C', 'M';app
'C' menaing Class, it should write in a class="superman",ide
'M' meaning Comment, it should wirte in a comment <!-- superman -->spa
Usually, Attribute directive is more common used than element directive!rest
Keyword:code
template: Accept a html code, can be "<div>...</div>", or a html filehtm
link: return a function to the browser.blog
In directive, you can just return a fucntion:ip
app.directive('enter', function(){ return function(scope, element){ element.bind("mouseenter", function(){ console.log("I am inside you"); }); } }); app.directive('leave', function(){ return function(scope, element){ element.bind("mouseleave", function(){ console.log("I am leaving"); }); } });
<!DOCTYPE html> <html> <head> <title>Angular Directive</title> <script src="angular.min.js"></script> <script src="main.js"></script> </head> <body ng-app="behaviorApp"> <div superman></div> <div enter leave>I'm content!</div> </body> </html>
By default, if you don't say restrict, it is:element
restrict: 'A'