javascript – Why and how to use directives in AngularJS?


At the moment I am studying Angular and, to be honest, I do not understand what directives are for and where to apply them. In my current understanding, this is just a piece of code and a template, but then why not use, for example, ng-inject and work in it using a controller. In general, please tell me, experienced people, where and how and in what form it is correct and reasonable to apply the directives? It will be doubly useful if you also throw links to examples.


As the author of these articles writes Directives in Angularjs for Beginners "In my opinion, directives are the main highlight of Angularjs' declarative style."

Directives allow you to make your code more descriptive and readable. Directives can be very flexible and have a number of functional features – to customize the scope inside the directive, run your code at different stages of the directive (compile – pre and post, link).

Scroll to Top