Reputation: 184
I'm currently preparing for a big project in angularJS, and I'm really confused with what angularJS directives is for?
Just a clarification, or an example will really help alot.
Upvotes: 1
Views: 142
Reputation: 3327
You should have a look at this in the first place: http://blog.angularjs.org/2012/11/about-those-directives.html
Maybe you could think about directives like little macros which improve classic HTML syntax.
Directives are used to replace static HTML elements with more modular, reusable parts of functionality.
Think of a more-or-less complex form which should be dynamic and which you would like to avoid to retype all the time. That would be a pretty good scenario for declaring a directive.
Directives are further the only place to interact with the DOM
Of course that's just a very basic example. Directives are far more than shorthands for the lazy programmer :). They can add heavy complexity and functionality to otherwise static elements and are a good way for implementing DRY code.
Edit:
You can use directives not only by creating custom elements like <myDirective>
but also by restricting them to attibutes or classes. You could then link a function to them:
app.directive('myDirective',function(){
return {
restrict: 'A', // Attribute
link: function(){
// do something great
}
}
})
This and other interesting stuff about directives (as well as many more topics) is pretty well explained at egghead.io
Further reading and watching:
http://docs.angularjs.org/guide/directive
http://blog.brunoscopelliti.com/use-cases-of-angularjs-directives
http://www.codinginsight.com/angularjs-directives/
https://github.com/angular/angular.js/wiki/Understanding-Directives
http://www.befundoo.com/university/tutorials/angularjs-directives-tutorial/
http://www.egghead.io/
Upvotes: 2
Reputation: 12531
You should take a look first at the documentation: http://docs.angularjs.org/guide/directive Then take a look at some examples, like this one which focus only on directives: http://www.youtube.com/watch?v=Yg-R1gchccg
Upvotes: 2