J
J
John2016-02-10 11:30:24
Angular
John, 2016-02-10 11:30:24

Give examples of using custom directives in AngularJS?

Friends, I reviewed a bunch of sites, but nothing came to me. Can you give some simple examples when you need to write your own directives?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2016-02-10
@AMar4enko

A button that changes appearance for the duration of some long action. The dropdown menu for the element.
A text field that adjusts the height to fit the content. Input field with add. design and behavior (as in Material Design, for example)

L
lega, 2016-02-10
@lega

A directive is, roughly speaking, a function (a piece of code/logic) "tied" to an element.
1) If you need to do something with the element
2) or if you have logic in the controller for working on the element (ngStyle/ngClass,ngClick...) and you need to reuse this logic in different places, then you can make a directive and use it so as not to copy-paste the code.
Here is an example of a simple Angular Light directive: jsfiddle.net/lega911/vrfdqbu8
Try to make an analogue with ngStyle, you will have to fiddle with variables for each case (although you can isolate controllers), in general, more code and copy-paste.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question