AngularJS Directives

AngularJS directives are a cool way of defining behavior and using it any way you want. You can have a directive with your name and have it do any kind of work you want depending on the situation. AngularJS directives control your DOM behavior.

Directives

Directives guide the HTML compiler to control the elements so they can behave in a certain way. The rendering of HTML inside the AngularJS application is controlled by the directives. Depending on the type of directives, they can be used to change the behavior.  Think about it like the word ‘change’. You can use it to refer to ‘change of clothes’ or ‘spare change’. It depends on the context. Similarly, Directives too can be used to behave differently if used in different contexts.




Built-in Directives

Here are some key details about directives.

AngularJS has some crucial inbuilt directives which can make your application behave in a certain way. Directives can be integrated with the HTML or can be used in the Javascript.

One thing which is cool about directives is its naming. The naming of a directive follows camel case when used in Javascript: (ngApp). Whereas, if the directives are used in a HTML, the name is separated with a hyphen: (ng-app).

There are four basic directives:

  1. ng-app : To auto-bootstrap and initialize an AngularJS application
  2. ng-model : To bind between HTML control and application data
  3. ng-init : To initialize application data
  4. ng-view : To switch between different views

There are many builtin directives, here are my favorites:

  1. ng-controller : To attach a controller class to the view
  2. ng-bind : To replace the text of the element with value of an expression
  3. ng-repeat : To instantiate a template once per item from a collection
  4. ng-hide : To show or hide the given element based on given expression

Directives are defined in a similar way as the Controllers and services. They can be defined using app.directive method. The method takes a name of the directive and a function to define what happens in the directive.




Types of Directives

The four types of Directives are classified based on where they are applied. For example, let ‘kuul-stuff ‘ be the directive you wrote. Then,

  1. Attribute directives : The directive is used to change the attribute of an element                                                                                                            <span kuul-stuff=”expression”></span>
  2. Element directives: The directive itself is used as an element              <kuul-stuff></kuul-stuff>
  3. Class directives: The directive is used as a class                                       <span class=”kuul-stuff: exp;”></span>
  4. Comment directives: The directive is used in the comments section                <-directive: kuul-stuff exp ->

Custom Directives

Custom directives are a cool way of defining your own functionality or workflow. They enhance existing directives and are easy to integrate in the HTML. The custom directives are one of the super powers of AngularJS that makes it build powerful applications. Here are some details about custom directives.

Custom directives are defined using app.directive method. The directive function returns an object in its callback.

myapp.directive (‘myDirective’, function(){

—–     write your functionality—-

});

There are some options which must be specified while writing a directive.

  1. Restrict: Defines the type of directive<br>’A’ = Attribute directive, ‘E’ = Element directive, ‘C’ = Class directive, and ‘M’ = Comment directive. It is better to define Attribute or Element directive so you can determine what element a directive matches
  2. Template: Defines directive output content
  3. TemplateURL: Gives the path of the template to be shown
  4. Controller: Defines a controller in the directive which can be used to achieve a specific task.
  5. Controller As: Defines an alias for the controller so you can refer to the controller’s variables using the alias
  6. Scope: Defines the scope of the directive                                                            ‘false‘ = Default value, which means that the scope of the directive is shared by the scope of the parent. There is no local scope present    ‘true‘ = It means that a new scope is created for the directive. There is local scope present as well as we can access the parent scope.  ‘{}‘=Isolated scope means that there is no link between parent and directive scope. A new scope is created and cannot be accessed outside. Further, the directive cannot access the outside scope.                    It is suggested to have an isolated scope in large AngularJS applications to keep the data safe. However, isolated scope can be accessed outside using some techniques. There is some magic which will be revealed later. Okay, fine. Here is the key. Use the following property to achieve the given purpose.                                                                                                             ‘=’ = Handles two-way binding.                                                                     ‘@’=Passing string values to directives.                                                      ‘&’=External function invocation.
  7. Compile: Compile method is specified to achieve a function as soon as the page loads. It will return a link function. If the behavior occurs only once in the directive, use compile.
  8. Link: Link method is specified to achieve a function similar to compile but it loads after compile. If the behavior occurs multiple times in the directive, use Link.

The order the AngularJS guides the HTML compiler is:                                    Compile–>Controller–>Pre-link–>Link–>Post-link.                                             It depends on what you want to achieve in the directive.




Directive Example

Here is an example of a custom directive.

myapp.directive(‘kuulStuff’, function(){

return {

Restrict: A,

template: <span> Hey, I’m custom</span>,

templateUrl: ‘/kuulstuff/home.html’,

Scope: ‘true’,

Compile: function(telement,tattrs) {

console.log(“Hi, I’m custom”);

var linkfn=function($scope, element,attrs){

element.css(‘background-color, ‘red’);

}

return linkfn;

}),

controller: function(){

console.log(“In the controller”);

},

Link: function(scope, telement,tattrs){

console.log(“In the link”);

},

};

});

This is just ‘some’ information about directives in AngularJS. It may be a bit confusing in the beginning, but if you write more, you will be fine. Write your own directives and let me know how it goes. 🙂