- Create a directive
- Register it.
- Use it into View
Let's understand how to create a new custom directive. We do create a directive which simply highlights an element whenever the mouse hovers over it.
For this, we create a new folder called “directives”. In this folder, we create a file called “element-highlight.directive.ts” file. Inside the file, write the below code,
- import { element } from 'protractor';
- import { Directive, OnInit, ElementRef } from "@angular/core";
- @Directive({
- selector:'[elementHighlight]'
- })
- export class ElementHighlightDirective implements OnInit{
- constructor( private eleRef:ElementRef){}
- ngOnInit(){
- this.eleRef.nativeElement.style.backgroundColor='red';
- this.eleRef.nativeElement.style.color='white';
- }
- }
- import { ElementHighlightDirective } from './directives/element-highlight.directive';
- declarations: [
- AppComponent,
- ElementHighlightDirective
- ],
- <div style="text-align:center">
- <h1>
- Custom Directive
- </h1>
- </div>
- <div class="row">
- <p elementHighlight>Mahesh Verma</p>
- </div>
Now, save and run the project and see the output.

After seeing the above output, we can say that our directive is working.
Use of @HostListner in Directives
Suppose, in the directive we want that whenever we hover the mouse over the div then its color changes into red, and when the mouse leaves it again it looks simple, then we use two events (mouseenter and mouseleave) with the help of @HostListner.
- export class ElementHighlightDirective implements OnInit{
- constructor( private eleRef:ElementRef){}
- ngOnInit(){
- }
- @HostListener('mouseover') mouseover(eventData:Event){
- this.eleRef.nativeElement.style.backgroundColor="Red";
- this.eleRef.nativeElement.style.color="White";
- }
- @HostListener('mouseleave') mouseleave(eventData:Event){
- this.eleRef.nativeElement.style.backgroundColor="transparent";
- this.eleRef.nativeElement.style.color="Black";
- }
- }

In this function, we can pass only one string defining to which property of the hosting element we want to bind or, we can say, decorator, takes one parameter i.e. the name of the host element property whose value we want to assign in the directive.
Let’s understand with the help of an example, we can achieve the above-mentioned feature by making changes in directive property and binding it with HostBinding.
Change the directive code as following,
- export class ElementHighlightDirective implements OnInit{
- constructor( private eleRef:ElementRef){}
- ngOnInit(){
- }
- @HostBinding('style.backgroundColor')backgroundColor:string="transparent";
- @HostListener('mouseover') mouseover(eventData:Event){
- this.backgroundColor="Red";
- this.eleRef.nativeElement.style.color="White";
- }
- @HostListener('mouseleave') mouseleave(eventData:Event){
- this.backgroundColor="transparent";
- this.eleRef.nativeElement.style.color="Black";
- }
- }
Can we pass data from directives?
So, the answer is Yes, we can pass the data from directives by using the binding property and @Input(). Let’s understand this with the help of an example.
Go to the “element-highlight.directive.ts” and change directive as per the following code,
- export class ElementHighlightDirective {
- @Input() defaultColor:string="";
- @Input() highlightColor:string="";
- @HostBinding('style.backgroundColor') backgroundColor:string=this.defaultColor;
- constructor( private eleRef:ElementRef){}
- @HostListener('mouseover') mouseover(eventData:Event){
- this.backgroundColor=this.highlightColor;
- this.eleRef.nativeElement.style.color="White";
- }
- @HostListener('mouseleave') mouseleave(eventData:Event){
- this.backgroundColor=this.defaultColor;
- this.eleRef.nativeElement.style.color="Black";
- }
- }
Now, pass the value of these properties from directive:
- <div class="row">
- <p elementHighlight [defaultColor]="'transparent'" [highlightColor]="'Red'">Mahesh Verma</p>
- </div>
Save and run the project, the output is:

For this write the following code into your “element-highlight.directive.ts”
- export class ElementHighlightDirective {
- constructor(private eleRef: ElementRef) { }
- @HostListener('blur') blur(eventData: Event) {
- let val: string = "";
- val = this.eleRef.nativeElement.value;
- if (val.length > 0) {
- val = val.match(new RegExp('.{1,4}', 'g')).join('-');
- }
- this.eleRef.nativeElement.value = val;
- }
- }
- <div class="row">
- <input type="text" placeholder="Enter any number" elementHighlight />
- </div>

In the above code, we have not applied any kind of validation or other things, we can modify as per our requirements, here we got to know how we can use directives.
As we know, directives are a very important concept in Angular and most of the time we need to create directives as per our requirement. I hope you have learned something about custom directives from this article. For practice purposes, I have attached a sample project called “CustomSampleDirectives” which covers all the code. You can download and modify this as per your requirement. All your queries related to this article and sample project are always welcomed. Thanks for reading.

Soujanya BysaniPosted Nov 23, 2018, 1:36 AM
Very useful article....thanks:)
Reena LakraPosted Jun 29, 2018, 3:59 AM
Very informative and nice article. Anyone can easily understand the concept of attribute directive and able to create it.
Komal SharmaPosted Jun 29, 2018, 3:51 AM
Good work, keep post such articles
Bhawna KaushikPosted Jun 22, 2018, 3:29 AM
Very nice explanation sir.....
Shivendra ShuklaPosted Jun 21, 2018, 5:32 AM
Good job ,keep it up
Deepak VermaPosted Jun 21, 2018, 1:38 AM
Great explanation.......... :)
Bhairab DuttPosted Jun 20, 2018, 1:04 PM
Very informative about custom directive in Angular ....