Build Navigation In Angular App

Introduction

Today, there are several JavaScript frameworks and libraries available for developers but Angular is one of the most popular frameworks for building complex Web applications using JavaScript. Angular is the most versatile and complete JavaScript framework among them all. Web applications developed in Angular are fast, maintainable, and support progressive web and native features.

Angular also lets you build and use components to save you development time. Infragistics is one of the companies that provides built-in Angular components to put your Angular application on the fast track.

Website navigation is one of the common controls used on websites to provides the entire navigation of a website. Infragistics provides the Navigation Drawer control that can be used to quickly build the same functionality.

In this article, I will show you how to integrate a website navigation functionality to your web app using Infragistics’s Navigation Drawer.

In this article, we will learn the following,

  • Install Ignite UI for Angular
  • Create Angular Project with Ignite UI
  • Introduction to Navigation Drawer
  • Implement the navigation drawer
  • Adding child pages
  • Conclusion

Introduction To Navigation Drawer

Ignite UI provides a Navigation Drawer component for Angular that is a side navigation container. It can be placed above the content and also have a slide in/out of view or be pinned to expand/collapse within some content. This component is fully customizable and can use default menu item styling.

Install Ignite UI for Angular

First make sure, you have the latest version of node and npm installed. If you are new to npm, check out the npm documentation here https://docs.npmjs.com/cli/install.

Open the command prompt and run the following npm command to install Ignite UI.

npm install -g igniteui-cli

Create an Angular Project with Ignite UI

To create an angular project with Ignite UI, run the following command.

ng new nav-drawer-demo

Get in to the project using below command,

cd nav-drawer-demo

Now it’s time to install the Ignite UI for this project. Run the following command:

npm install igniteui-angular

After installing Ignite UI for our project, add the required Ignite UI styles and the HammerJs library in the angular-cli.json.

Open the .angular-cli.json from the root of the project and add below code snippet as shown Listing 1.

  1. "styles": [  
  2. "../node_modules/igniteui-angular/styles/igniteui-angular.css"  
  3. ]  
  4.   
  5. "scripts": ["../node_modules/hammerjs/hammer.min.js"]  

Listing 1.

Now we have configured Ignite UI with our angular project successfully.

Implement the Navigation Drawer

To add the Navigation Drawer, we need to add the dependencies.

Open the app.module.ts and import the component for IgxNavigationDrawerModule. See Listing 2.

  1. import { IgxNavigationDrawerModule} from 'igniteui-angular/main';  
  2. //Or  
  3. import { IgxNavigationDrawerModule} from 'igniteui-angular/navigation-drawer;  

Listing 2.

And also add the IgxNavigationDrawerModule. See Listing 3.

  1. @NgModule({  
  2.     declarations: [  
  3.         AppComponent  
  4.     ],  
  5.     imports: [  
  6.         IgxNavigationDrawerModule,  
  7.     ],  
  8.     providers: [],  
  9.     bootstrap: [AppComponent]  
  10. })  

Listing 3.

To add the html markup, open the app.component.html and write the code in Listing 4.

  1. <div class="content-wrap">  
  2.     <igx-nav-drawer id="navigation" #drawer [isOpen]="true">  
  3.         <ng-template igxDrawer>  
  4.             <nav> <span igxDrawerItem [isHeader]="true"> Dashboard</span> <span igxDrawerItem igxRipple> Edit Profile</span> <span igxDrawerItem igxRipple [active]="true"> View Profile</span> <span igxDrawerItem igxRipple> Setting</span> <span igxDrawerItem [isHeader]="true"> Privacy</span> <span igxDrawerItem igxRipple> Photo Uploads</span> <span igxDrawerItem igxRipple> Logout </span> </nav>  
  5.         </ng-template>  
  6.     </igx-nav-drawer>  
  7.     <main>  
  8.         <!-- <button (click)="drawer.toggle()"> Menu </button> --><span igxButton="icon" igxToggleAction="navigation" [closeOnOutsideClick]="false">  
  9. <igx-icon fontSet="material" name="menu"></igx-icon>  
  10. </span> </main>  
  11. </div>  

Listing 4.

The above code snippet and is enough to show a basic navigation drawer with minimum functionality in a web page.

Now, let’s use the following command.

ng serve

And open the web application in a web browser using http://localhost:4200.

Angular
Figure 1.

As you can see from Figure 1, a web page with simple navigation in the left side is created.

Add the child page

We just added a simple navigation to a web page in the above section. Now, let’s attach some child page links to these navigation links.

Create three new components named EditProfile , ViewProfile, NotFound using the following commands.

ng generate component edit-profile

ng generate component view-profile

ng generate component not-found

Add three HTML files with simple text in them. These will be three pages that we will link to the link in the navigation control.

“edit-profile.component.html”

  1. <h1 style="text-align:center">Edit Profile Page</h1>  

 

“view-profile.component.html”

  1. <h1 style="text-align:center">View Profile Page</h1>  

“not-found.component.html”

  1. <h1 style="text-align:center">Page Not Profile Page</h1>  

Add the Routing for the pages

Now, let’s add routing import the following modules in Listing 5.

  1. import { RouterModule, Routes } from '@angular/router';  

 

Also add the following code shown in listing 10.

  1. const appRoutes: Routes = [{  
  2.     path: 'not-found',  
  3.     component: NotFoundComponent  
  4. }, {  
  5.     path: 'edit-profile',  
  6.     component: EditProfileComponent  
  7. }, {  
  8.     path: 'view-profile',  
  9.     component: ViewProfileComponent  
  10. }];  
  11. @NgModule({  
  12.             declarations: [  
  13.                 imports: [  
  14.                     IgxNavigationDrawerModule,  
  15.                     RouterModule.forRoot(appRoutes)  
  16.                 ],  
  17.                 providers: [],  
  18.                 bootstrap: [AppComponent]  
  19.             })  

Listing 5.

Now open the app.component page and add the following code snippet to inject the router in constructor.

  1. import {  
  2.     Router  
  3. } from '@angular/router';  
  4. constructor(private router: Router) {}  

Listing 6.

Now we have enabled the routing in our project.

Let’s add the RounterLink in the navigation Drawer so we can open the edit and view profile pages in the main area of the navigation drawer.

Open the app.components.html page and add the following code snippet in Listing 7.

  1. <div class="content-wrap">  
  2.     <igx-nav-drawer id="navigation" #drawer [isOpen]="true">  
  3.         <ng-template igxDrawer>  
  4.             <nav> <span igxDrawerItem [isHeader]="true"> Dashboard</span> <span igxDrawerItem igxRipple routerLink="/edit-profile">Edit Profile</span> <span igxDrawerItem igxRipple routerLink="/view-profile">View Profile</span> <span igxDrawerItem igxRipple routerLink="/not-found"> Setting</span> <span igxDrawerItem [isHeader]="true"> Privacy</span> <span igxDrawerItem igxRipple routerLink="/not-found"> Photo Uploads</span> <span igxDrawerItem igxRipple routerLink="/not-found"> Logout </span> </nav>  
  5.         </ng-template>  
  6.     </igx-nav-drawer>  
  7.     <main> <span igxButton="icon" igxToggleAction="navigation" [closeOnOutsideClick]="true">  
  8. <igx-icon fontSet="material" name="menu"></igx-icon>  
  9. </span>  
  10.         <section>  
  11.             <div>  
  12.                 <router-outlet></router-outlet>  
  13.             </div>  
  14.         </section>  
  15.     </main>  
  16. </div>  

Listing 7.

Also add the additional components in app.module.ts page which are shown as highlighted in Listing 8.

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import {  
  4. IgxButtonModule,  
  5. IgxIconModule,  
  6. IgxNavigationDrawerModule,  
  7. IgxRippleModule,  
  8. IgxToggleModule } from 'igniteui-angular/main';  
  9.   
  10. import { AppComponent } from './app.component';  
  11. import { RouterModule, Routes } from '@angular/router';  
  12. import { EditProfileComponent } from './edit-profile/edit-profile.component';  
  13. import { ViewProfileComponent } from './view-profile/view-profile.component';  
  14. import { NotFoundComponent } from './not-found/not-found.component';  
  15.   
  16. const appRoutes: Routes = [  
  17. { path: 'not-found', component: NotFoundComponent },  
  18. { path: 'edit-profile', component: EditProfileComponent },  
  19. { path: 'view-profile', component: ViewProfileComponent }  
  20. ];  
  21.   
  22. @NgModule({  
  23. declarations: [  
  24. AppComponent,  
  25. EditProfileComponent,  
  26. ViewProfileComponent,  
  27. NotFoundComponent  
  28. ],  
  29. imports: [  
  30.    BrowserModule,  
  31.    IgxButtonModule,  
  32.    IgxIconModule,  
  33.    IgxNavigationDrawerModule,  
  34.    IgxRippleModule,  
  35.    IgxToggleModule,  
  36.    RouterModule.forRoot(  
  37. appRoutes  
  38. )  
  39. ],  
  40. providers: [],  
  41.    bootstrap: [AppComponent]  
  42. })  
  43. export class AppModule { }  

Listing 8.

One last thing. Add the following CSS in app.component.css.

  1. .content-wrap {  
  2. width: 100%;  
  3. height: 100%;  
  4. display: flex;  
  5. }  

Listing 9.

Now run the application and run the following command.

ng serve

And open the web application in web browser using http://localhost:4200

Angular
Figure 2.

Angular
Figure 3.

Conclusion

Navigation drawer is a very useful component of Ignite UI to quickly add navigation to your Angular app. In this article, we saw how to add a navigation page and attach navigation links to the control items. The control is responsive and works flawlessly on any device of any size.

Learn more, download a free trial, and get started here,


Similar Articles