How to Use & Create custom directive in Angular?
Last Updated :
20 Mar, 2024
Directives in Angular are nothing but the classes that allow us to add and modify the behavior of elements. Using directives in angular we can modify the DOM (Document Object Module) styles, handle user functionality, and much more.
Custom Directives:
A custom directive in Angular is a user-defined directive that extends the functionality of HTML by introducing new behaviors or attributes. Unlike built-in directives provided by Angular, custom directives are created to encapsulate specific functionality and promote code reuse within Angular applications.
Steps to create the Directive:
Step 1: Create the angular project using the following command.
ng new custom-directive
cd custom-directive
Step 2: To generate a new directive, we can use the following command.
ng generate directive sampleColorChange
Folder Structure:

Dependencies:
"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/platform-server": "^17.3.0",
"@angular/router": "^17.3.0",
"@angular/ssr": "^17.3.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "^17.3.0",
"@angular/cli": "^17.3.0",
"@angular/compiler-cli": "^17.3.0",
"@types/express": "^4.17.17",
"@types/jasmine": "~5.1.0",
"@types/node": "^18.18.0",
"jasmine-core": "~5.1.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.4.2"
}
Example: Implementing Custom directive
HTML
<!-- app.component.html -->
<div appSampleColorChange>Text using directive</div>
JavaScript
//sample-color-change.directive.ts
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[appSampleColorChange]',
})
export class SampleColorChangeDirective {
constructor(private el: ElementRef) {
this.el.nativeElement.style.color = 'blue';
this.el.nativeElement.style.fontWeight = 'bold';
}
@HostListener('mouseleave') onMouseLeave() {
this.el.nativeElement.style.backgroundColor = 'red';
}
}
To start the application run the following command.
ng serve
Output:
Without any mouse events
After removing the mouse from the textExample 2: We can also pass multiple inputs to our custom directive. Let us understand it by taking an example.
HTML
<!-- app.component.html -->
<div appSampleColorChange [color]="'red'" [backgroundColor]="'blue'">
Text using directive
</div>
JavaScript
//sample-color-change.directive.ts
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[appSampleColorChange]',
})
export class SampleColorChangeDirective {
@Input() color: string = '';
@Input() backgroundColor: string = '';
constructor(private el: ElementRef) {
this.el.nativeElement.style.fontWeight = 'bold';
}
@HostListener('mouseenter') onMouseEnter() {
this.el.nativeElement.style.color = this.color;
this.el.nativeElement.style.backgroundColor = this.backgroundColor;
}
}
Output:

On hovering the textBenefits of custom directives :
- The main advantange of custom directives is the code reusability . Instead of writing the code repeatedly , we can create one directive and use it wherever required.
- This also allows us to seperate the complex code into directives, and make it more readable.
- Custom directives helps us to validate data and make custom changes to UI based on certain conditions, which makes code flexible.
- They also helps in code consistency.
Similar Reads
How to create a custom pipe in AngularJS ?
In this article, we will learn how to generate custom pipe other than using built-in pipe in Angular & also explore its implementation. The Pipe is a function that is used to modify the data before rendering it to the user. Some of the pre-built pipes are date, uppercase, lowercase, currency, de
6 min read
How to pass input to a custom directive in Angular JS?
In AngularJS, passing input to a custom directive allows the customization of components within an application. Directives can receive input via attributes, expressions, or controller functions, allowing for dynamic behavior and data binding. In this article, we will explore two different approaches
3 min read
What is a custom directive in Angular?
Angular, a popular framework for building dynamic web applications, offers a powerful feature known as custom directives. These directives extend the functionality of HTML elements, enabling to create reusable components and add behavior to their applications. In this article, we'll learn about the
4 min read
Attribute Directives in Angular
Attribute directives are a powerful tool that allows you to manipulate the behavior and appearance of HTML elements. In this article, you will see the fundamentals of attribute directives. Table of Content What are Attribute Directive?Benefits of Attribute DirectiveTypes of Attribute DirectivesSteps
5 min read
Create A Template Driven Form In Angular
Template Driven Form in Angular means managing form input and validation primarily through HTML templates, using Angular directives like ngModel for two-way data binding and ngForm for form management. This approach is ideal for simpler forms where most of the logic and validation are handled direct
4 min read
What are Directives in AngularJS ?
AngularJS directives are extended HTML attributes having the prefix ng-. Directives are markers on the DOM element which tell Angular JS to attach a specified behavior to that DOM element or even transform the DOM element with its children. During compilation, the HTML compiler traverses the DOM mat
7 min read
How to use bootstrap 4 in angular 2?
Bootstrap is an open-source toolkit for developing with HTML, CSS, and JS. The Bootstrap framework can be used together with modern JavaScript web & mobile frameworks like Angular. Bootstrap 4 is the newest version of Bootstrap, which is the most popular HTML, CSS, and JavaScript framework. This
2 min read
AngularJS ng-cut Directive
The ng-cut Directive in AngularJS is used to specify the custom behavior functions when the text in input fields is cut. It can be used when we want to call a function that will be triggered when the text is cut from the input field. It is supported by all input elements. Syntax: <element ng-cut=
2 min read
AngularJS ng-class-odd Directive
The ng-class-odd Directive in AngularJS is used to specify the CSS classes on every odd appearance of HTML elements. It is used to dynamically bind classes on every odd HTML element. If the expression inside the ng-class-odd directive returns true then only the class is added else it is not added. T
2 min read
How to create a new component in Angular?
A component in Angular is the building block for making web pages. It is a reusable block of code that can be used anywhere in the app any number of times. It provides scalability, reusability, and readability. Each component does a specific job like showing a menu, a photo or a card, etc. In this a
3 min read