Navigating Between Two Screens And Applying Directive For Dropdown

Introduction

 
Hi guys, I want to share how we can implement redirection from one screen to other screen in Angular 6 and explain creating and applying custom directives to a dropdown with an example.

If you are a developer looking for a chunk of code to implement in your project, then you are at the right place!!

So lets begin with the code!
 
Key points to learn:
  • HostBinding
  • HostListener
  • ElementRef
  • EventEmitter
  • Output
  • applying directive from shared file to components 
Step 1
 
Create the required components in the project. I have created below components with the help of the Angular CLI command.
  1. ng g c employees  
  2. ng g c departments  
  3. ng g c employees
  4. ng g c header 
Step 2
 
Open the header.html component and paste the below code, we are applying the directive with the reference name appDropdown in the below code.
  1. <nav class="navbar navbar-default">  
  2.   <div class="container-fluid">  
  3.       <div class="navbar-header">  
  4.           <a href="#" class="navbar-brand">company Logo</a>  
  5.       </div>  
  6.       <div class="collapse navbar-collapse">  
  7.           <ul class="nav navbar-nav">  
  8.               <li><a href="#" (click)="onSelect('Emp')">Employees</a></li>  
  9.               <li><a href="#" (click)="onSelect('Dept')">Department</a></li>  
  10.           </ul>  
  11.           <ul class="nav navbar-nav navbar-right">  
  12.               <li class=dropdown appDropdown>  
  13.                   <a href="#" class="dropdown-toggle" role="button">More <span class="caret"></span></a>  
  14.                   <ul class="dropdown-menu">  
  15.                       <li><a href="#">About Company</a></li>  
  16.                       <li><a href="#">Contact Us </a></li>  
  17.                   </ul>  
  18.               </li>  
  19.           </ul>  
  20.       </div>  
  21.   </div>  
  22. </nav> 
Step 3
 
Create a dropdown.directive.ts shared file to apply dropdown functionality. Open the dropdown.directive.ts file and paste the below code. We are using the selector appDropdown from the below code and adding it to dropdown in step 2. Make sure this file is added in module.ts because it is not created by the CLI command file. We are creating this file because we can apply this functionality in the multiple components dropdown which can reduce repeated code in the project.
  1. import { Directive, HostBinding, HostListener, ElementRef } from '@angular/core';  
  2.   
  3. @Directive({  
  4.     selector:'[appDropdown]'  
  5. })  
  6. export class DropdownDirective{  
  7. @HostBinding('class.open') isOpen=false;  
  8. @HostListener('document:click',['$event']) toggleOpen(event:Event){  
  9.     this.isOpen=this.elRef.nativeElement.contains(event.target)? !this.isOpen:false;  
  10.     //this.isOpen=!this.isOpen;  
  11.    }  
  12.    constructor(private elRef:ElementRef){  
  13.   
  14.   }  
  15. }  
Step 4
 
Open the header.ts file and paste the below code. We are implementing event emitter and @output.
  1. import { Component, OnInit, EventEmitter, Output } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-header',  
  5.   templateUrl: './header.component.html',  
  6.   styleUrls: ['./header.component.scss']  
  7. })  
  8. export class HeaderComponent {  
  9.   @Output() featureSelected=new EventEmitter<string>();  
  10.   onSelect(feature: string){  
  11.       this.featureSelected.emit(feature)  
  12.   }  

Step 5
 
Open app.Component.html file and paste the below code. featureSelected is importing from the header.html file
  1. <app-header (featureSelected)="onNavigate($event)"></app-header>  
  2. <div class="container">  
  3. <div class="row">  
  4.     <div class="col-md-12">  
  5.         <app-employees *ngIf="loadFeature==='Emp'"></app-employees>  
  6.         <app-departments *ngIf="loadFeature!=='Emp'"></app-departments>  
  7.     </div>  
  8.   </div>  
  9. </div> 
Step 6
 
Open the app.Component.ts file and paste the below code.
  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-root',  
  5.   templateUrl: './app.component.html',  
  6.   styleUrls: ['./app.component.scss']  
  7. })  
  8. export class AppComponent {  
  9.   loadFeature="Emp";  
  10.   onNavigate(feature: string){  
  11.     this.loadFeature=feature;  
  12.   }  

Step 7
 
Add some text in department.html and employees.html files to check the output and run the code with the below command:
  1. ng serve --port 4500 
Below should be the output:
 
First screen

Navigating Between Two Screens And Applying Directive For Dropdown
 
Second screen
 
Navigating Between Two Screens And Applying Directive For Dropdown
 

Summary

 
In this blog, you have learned how to navigate from one screen to another screen and apply the shared directive functionality to the components.
 
Hope this code help you. Please share your suggestions and comments below. Thanks!