What are directives in Angular JS?

In AngularJS, a directive is a special marker or instruction in the DOM (Document Object Model) that extends HTML with custom behavior. Directives are one of the most powerful features of AngularJS and are used to manipulate the DOM, create reusable components, and bind data to elements in a dynamic and declarative way.

Key Points about Directives in AngularJS:
Extending HTML Syntax: Directives allow you to add custom attributes, elements, and behavior to your HTML. By doing so, you can extend HTML with new capabilities, making it more expressive and dynamic.

Directives as Markup: In AngularJS, directives are used in the HTML markup to add functionality. They can manipulate DOM elements, bind data, or react to events.

Types of Directives:

  • Element Directives: These are custom HTML tags. For example:<my-custom-element></my-custom-element>
  • Attribute Directives: These are attributes that are applied to existing HTML elements. For example:<div ng-class=”{‘highlighted’: isHighlighted}”></div>
  • Class Directives: These are applied to elements with a specific class name.
  • Comment Directives: These can be applied to comment nodes in the HTML (rarely used).

Common Directives: AngularJS comes with several built-in directives that are commonly used for creating dynamic and interactive user interfaces. Some of the most frequently used directives include:

  • ng-model: Binds the value of an HTML form element (input, select, textarea) to a property in the model.
  • ng-repeat: Loops through an array or object and repeats HTML for each item in the collection.
  • ng-if: Conditionally includes or excludes HTML elements from the DOM.
  • ng-click: Specifies behavior when an element is clicked.
  • ng-show / ng-hide: Shows or hides an HTML element based on a condition.

Custom Directives: You can also create your own custom directives in AngularJS. This allows you to encapsulate reusable behavior and logic, making your application more modular and maintainable. A custom directive is created using the angular.module().directive() method.

Example of a Custom Directive:
angular.module(‘myApp’, [])
.directive(‘greeting’, function() {
return {
restrict: ‘E’, // ‘E’ means the directive is used as an element
template: ‘<h1>Hello, {{ name }}!</h1>’,
controller: function($scope) {
$scope.name = ‘AngularJS Developer’;
}
};
});
In the example:

  • restrict: ‘E’: Specifies that this directive should be used as an element, e.g., .<greeting></greeting>.
  • template: Defines the HTML content for the directive.
  • controller: Defines the controller logic for the directive, where you can manage the scope and business logic.

Using the Directive in HTML:
<greeting></greeting>
This will render:
<h1>Hello, AngularJS Developer!</h1>

Directive Lifecycle:

  • Compilation: The directive’s template is compiled and linked to the DOM during the directive’s creation.
  • Linking: The linking function is used to attach behaviors to DOM elements and set up scope bindings.

Advantages of Using Directives:

  • Code Reusability: Directives allow you to create reusable and modular UI elements, improving maintainability.
  • Declarative Programming: Directives provide a declarative way to express behavior in the DOM, making it easier to understand and manage.
  • DOM Manipulation: Directives can modify or extend the DOM in a structured manner, allowing for dynamic changes to the user interface.
  • Separation of Concerns: Directives separate HTML structure from behavior, promoting cleaner code and better organization.

In AngularJS, directives are used to extend HTML with custom elements and attributes that add functionality, manipulate the DOM, and bind data dynamically. Directives help in creating reusable, modular components and behaviors, making AngularJS applications more maintainable and scalable. AngularJS provides many built-in directives like ng-model, ng-repeat, and ng-if, and allows developers to define custom directives to meet specific needs.