Unleash The Potential Of Custom Dropdown Directive In Angular2

In this post, we will learn how to create our own custom directives. We can do it by simply importing Bootstrap Java Script files but if we are writing our own JavaScript then there is no need to import the bootstrap.js file. Instead of this, we are creating our own custom dropdown, using directive.

In this post, I am using Bootstrap to design the menu, so let's start creating. 

Directive

cmd> ng g d customdropdown

In customdropdown.directive.ts file, it is shown as follows.
  1. [code language = "typescript"]  
  2. import {  
  3.     Directive,  
  4.     HostListener,  
  5.     HostBinding  
  6. } from '@angular/core';  
  7. @Directive({  
  8.     selector: '[appcustomdropdown]'  
  9. })  
  10. export class CustomdropdownDirective {  
  11.     constructor() {}  
  12.     @HostBinding('class.open') get opened() {  
  13.         return this.isOpen;  
  14.     }  
  15.     @HostListener('click') open() {  
  16.         this.isOpen = true;  
  17.     }  
  18.     @HostListener('mouseleave') close() {  
  19.         this.isOpen = false;  
  20.     }  
  21.     private isOpen = false;  
  22. }  
When I click the dropdown menu, it will open up a menu for me. When I mouse out of it, it will close for me. If you want to change the menu behavior, click mouse over then simply you need to change the 'click' to 'mouseenter' inside the @HostListener.

Inside app.module file we need to declare custom directive inside the Declaration Array
  1. declarations: [  
  2.     AppComponent,  
  3.     CustomdropdownDirective  
  4. ]  
Now, to apply the directive, we need to create header component, as shown below.

cmd> ng g c header

It will generate header.component.ts,header.component.html,header.component.css.
Now, in header.component.html we will simply paste my header menu, which will design, using Bootstrap.

Thus, .html file will look, as shown below.
  1. [code language = "html"] < nav class = "navbar navbar-inverse" > < div class = "container-fluid" >  
  2.     <!-- Brand and toggle get grouped for better mobile display -->  
  3.     < div class = "navbar-header" > < button type = "button"  
  4. class = "navbar-toggle collapsed"  
  5. data - toggle = "collapse"  
  6. data - target = "#bs-example-navbar-collapse-1"  
  7. aria - expanded = "false" > < span class = "sr-only" > Toggle navigation < /span> < span class = "icon-bar" > < /span> < span class = "icon-bar" > < /span> < span class = "icon-bar" > < /span> < /button> < a class = "navbar-brand"  
  8. href = "#" > Jinal Shah < /a> < /div>  
  9.     <!-- Collect the nav links, forms, and other content for toggling -->  
  10.     < div class = "collapse navbar-collapse"  
  11. id = "bs-example-navbar-collapse-1" > < ul class = "nav navbar-nav" > < li > < a href = "#" > Home < span class = "sr-only" > (current) < /span></a > < /li> < li > < a href = "#" > About < /a></li > < /ul> < ul class = "nav navbar-nav navbar-right" > < li class = "dropdown"  
  12. appcustomdropdown > < a class = "dropdown-toggle"  
  13. data - toggle = "dropdown"  
  14. role = "button"  
  15. aria - haspopup = "true"  
  16. aria - expanded = "false" > Dropdown < span class = "caret" > < /span></a > < ul class = "dropdown-menu" > < li > < a href = "#" > Action < /a></li > < li > < a href = "#" > Another action < /a></li > < li > < a href = "#" > Something  
  17. else here < /a></li > < li role = "separator"  
  18. class = "divider" > < /li> < li > < a href = "#" > Separated link < /a></li > < /ul> < /li> < /ul> < /div><!-- /.navbar - collapse -->  
  19.     < /div><!-- /.container - fluid-- > < /nav> [/code]  
Now, we can call the header component inside any component, using selector.

<app-header></app-header>

I hope you found this post helpful..Thank you for reading.