How to Create a Custom Directive in AngularJS
directive in AngularJS
happy patel

Happy Patel

24 July 2023
5 min read


What is a directive?

In AngularJS, a directive is a fundamental concept that allows you to extend the HTML vocabulary and create reusable components or behaviors. Directives are used to add new behavior to elements, attributes, classes, or comments in the HTML markup.

AngularJS provides a set of built-in directives such as ng-app, ng-controller, ng-model, etc. These directives enhance the functionality of HTML by binding data, controlling the flow, manipulating the DOM, handling events, and more.

Additionally, AngularJS allows you to create custom directives, which are defined using the directive function. A custom directive can be used to encapsulate a specific behavior, create a reusable component, or extend the functionality of existing HTML elements.

Here’s a simple example of a custom directive in AngularJS:

angular.module(‘myApp’, [])

  .directive(‘myDirective’, function() {

    return {

      restrict: ‘E’,

      template: ‘<h1>Hello, Directive!</h1>’



In the above code, a new directive named directive is defined. It is restricted to be used as an element (restrict: ‘E’) and provides a template that will be inserted into the DOM when the directive is used in the HTML markup.

To use this directive in HTML, you would include it as an element:


When AngularJS encounters this custom directive in the HTML, it will replace the directive element with the template defined in the directive definition.

Directives are powerful tools in AngularJS that enable you to extend HTML and create reusable components, making it easier to build complex web applications with enhanced functionality and structure.

Categories of a directive

In AngularJS, directives can be categorized into three main types based on how they are used and interact with the DOM:

Attribute Directives: These directives modify the behavior or appearance of an element by adding or manipulating attributes. They are denoted by an attribute on an HTML element. Examples of attribute directives in AngularJS include ng-model, ng-show, and ng-click.

Element Directives: These directives create new custom elements that encapsulate a specific behavior or functionality. They are denoted by an element name in the HTML markup. Custom element directives are typically used when you want to create reusable components. Examples of element directives in AngularJS include <my-directive>, <my-component>, etc.

Class Directives: These directives are applied to elements by using a specific CSS class. They add or modify the behavior or appearance of the element. Class directives are denoted by a CSS class in the HTML markup. Examples of class directives in AngularJS include ng-class, ng-disabled, etc.

What is a custom directive?

A custom directive, in the context of web development, refers to a feature provided by some frontend frameworks, such as Angular or Vue.js, that allows developers to extend the functionality of HTML by creating their own HTML attributes. These custom directives can be used to add specific behaviors or manipulate the DOM (Document Object Model) in a declarative manner.

In Angular, for instance, you can create custom directives using the @Directive decorator. A custom directive allows you to attach custom behavior to elements or components in the template. For example, you could create a custom directive to highlight certain elements when clicked or add custom validation to form inputs.

Here’s a basic example of a custom directive in Angular:

import { Directive, ElementRef, HostListener } from ‘@angular/core’;


  selector: ‘[appCustomDirective]’


export class CustomDirective {

  constructor(private el: ElementRef) { }

  @HostListener(‘click’) onClick() {

    // Add your custom behavior here, e.g., highlight the element when clicked = ‘yellow’;




<div appCustomDirective>

  Click me to see the custom directive in action!


When the element with the appCustomDirective attribute is clicked, the custom directive’s onClick method will be executed, and the background color of the element will change to yellow.

Custom directives provide a way to encapsulate complex or repetitive functionality, making it easier to reuse code across different parts of your application. They are a powerful feature that enhances the capabilities of HTML and allows developers to create more interactive and dynamic web applications.

How does a custom directive work?

The working of a custom directive can vary depending on the frontend framework or library being used. I’ll explain the general concept of how custom directives work in the context of Angular, as it’s a popular framework that heavily utilizes custom directives.

In Angular, custom directives are created using the @Directive decorator and are attached to specific HTML elements or components using a custom selector. The directive class contains the logic that defines how the directive behaves when applied to an element.

Here are the key steps in the lifecycle of a custom directive in Angular:

  1. Directive Creation: You create a custom directive by defining a TypeScript class with the @Directive decorator. This decorator specifies the selector for the directive, which is used to identify elements to which the directive should be applied.
  2. Directive Registration: To make the custom directive available throughout your application, you need to register it in the @NgModule or the component’s module where it will be used. This step is crucial to ensure that Angular recognizes and understands the custom directive.
  3. Element Matching: When Angular initializes a component or parses the template, it looks for elements with attributes that match the selector of the custom directive.
  4. Directive Instantiation: Whenever Angular encounters an element with the directive’s selector, it creates an instance of the directive class and associates it with the element.
  5. Element Interaction: The directive class can define various lifecycle hooks and methods, which allow you to interact with the element to which the directive is applied. For instance, you can use lifecycle hooks like ngOnInit, ngOnChanges, or custom event listeners like @HostListener to respond to element events or modify its behavior.
  6. Manipulating the DOM: Custom directives often involve manipulating the DOM to add or modify elements or apply specific styles. This can be done through the ElementRef, which provides access to the underlying DOM element.
  7. Cleanup and Destruction: When the directive’s associated element is removed from the DOM, Angular calls the necessary lifecycle hooks to clean up resources and prevent memory leaks.

By creating custom directives, you can extend the capabilities of HTML and make your templates more expressive and maintainable. They provide a way to encapsulate behavior, separate concerns, and promote code reusability, as you can apply the same behavior to multiple elements or components without duplicating code.

Overall, custom directives are a powerful tool in front-end development, allowing developers to build more interactive and dynamic web applications with ease.

Search Here

Top Categories







































are You looking for Job Opportunity?


Subscribe to Our Newsletter

No spam, notifications only about new Blog Posts, updates.