---
title: "How do you create custom directives in AngularJS?"  
description: "How do you create custom directives in AngularJS?"  
author: "Revati S Misra"  
published: 2023-04-18  
updated: 2023-10-05  
canonical: https://www.mindstick.com/forum/157881/how-do-you-create-custom-directives-in-angularjs  
category: "angular js"  
tags: ["angular js", "angularjs development"]  
reading_time: 3 minutes  

---

# How do you create custom directives in AngularJS?

How do you create [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [directives in AngularJS](https://www.mindstick.com/interview/33915/what-are-directives-in-angularjs)?

## Replies

### Reply by Aryan Kumar

Creating custom [directives](https://www.mindstick.com/blog/44/directives-in-asp-dot-net) in [AngularJS](https://www.mindstick.com/forum/33926/use-angularjs-without-scope-in-mvc) is a fundamental part of extending HTML with your own custom functionality. AngularJS allows you to define your own HTML tags or attributes, and when the browser encounters them, it invokes custom JavaScript code that you provide. Here's how you can create custom directives in AngularJS:

## 1. Create an AngularJS Module:

Start by creating an AngularJS module that will contain your custom directive(s). This is done using the **angular.module** function.

```plaintext
angular.module('myApp', []);
```

## 2. Define the Directive:

To define a custom directive, use the **directive** method of your AngularJS module. The **directive** method takes two arguments: the name of the directive and a factory function that returns a configuration object.

```plaintext
angular.module('myApp').directive('myDirective', function () {
  return {
    // Configuration options go here
  };
});
```

## 3. Configure the Directive:

Inside the configuration object, you specify various options for your directive:

**restrict**: Defines how the directive can be used. Common values are **'E'** (element), **'A'** (attribute), and **'C'** (class). You can also use a combination, like **'EA'**.

**template** or **templateUrl**: Defines the HTML template for your directive. You can provide the template directly as a string or load it from an external file using **templateUrl**.

**scope**: Defines the scope of the directive. By default, directives use the parent scope, but you can create an isolated scope to encapsulate the directive's behavior.

**controller**: Specifies a controller for the directive, which can contain business logic and functions.

**link**: Defines a function that is executed when the directive is linked to the DOM. This is where you can interact with the DOM and add event handlers.

## 4. Use the Directive in HTML:

Once your custom directive is defined, you can use it in your HTML code. Use the name of the directive as an element, attribute, class, or comment, depending on the **restrict** option you defined.

```plaintext
<my-directive></my-directive>
<!-- or -->
<div my-directive></div>
<!-- or -->
<div class="my-directive"></div>
```

## 5. Configure the Module:

Don't forget to include your custom directive module in your main AngularJS application module.

```plaintext
angular.module('app', ['myApp']);
```

## Example:

Here's an example of a simple custom directive that displays a message:

```plaintext
angular.module('myApp').directive('myDirective', function () {
  return {
    restrict: 'E',
    template: '<div>This is my custom directive!</div>',
  };
});
```

In your HTML, you can use this directive like this:

```plaintext
<my-directive></my-directive>
```

When AngularJS encounters **<my-directive></my-directive>**, it replaces it with the template defined in your custom directive configuration.

This is a basic introduction to creating custom directives in AngularJS. You can make directives more complex by adding controllers, transclusion, and more, depending on your requirements.


---

Original Source: https://www.mindstick.com/forum/157881/how-do-you-create-custom-directives-in-angularjs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
