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.
- ng g c employees
- ng g c departments
- ng g c employees
- ng g c header
Open the header.html component and paste the below code, we are applying the directive with the reference name appDropdown in the below code.
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <div class="navbar-header">
- <a href="#" class="navbar-brand">company Logo</a>
- </div>
- <div class="collapse navbar-collapse">
- <ul class="nav navbar-nav">
- <li><a href="#" (click)="onSelect('Emp')">Employees</a></li>
- <li><a href="#" (click)="onSelect('Dept')">Department</a></li>
- </ul>
- <ul class="nav navbar-nav navbar-right">
- <li class=dropdown appDropdown>
- <a href="#" class="dropdown-toggle" role="button">More <span class="caret"></span></a>
- <ul class="dropdown-menu">
- <li><a href="#">About Company</a></li>
- <li><a href="#">Contact Us </a></li>
- </ul>
- </li>
- </ul>
- </div>
- </div>
- </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.



Ohon RehmanPosted Oct 10, 2020, 1:19 AM
Great Navigating
Santhosh PatilPosted Oct 4, 2020, 9:58 AM
Good one.....