Nav Bar In Angular Using Ignite UI

Today, navigation is one of the most common functionalities in websites. Poor navigation may lead to user frustration and make visiting your website an un-engaging experience.  Research proves that if your website can’t engage a user without 2 minutes, that user may never visit your website again.

Navigation bar or nav bar is one of the controls used to drive web navigation. Nav bar provides the current position of a user while browsing a website and moves as the user moves from page to page. Nav bar also allows a user to go back and jump between pages.

Angular is one of the most popular frameworks for building Websites and building navigation may be time consuming. As a Web developer, when building navigation bars, you have two major choices. You can either create your own navigation or use an existing navigation control that is developed by industry experts and veterans. One such control is Infragistics’s Ignite UI for Angular’s Nav Bar. One of the key advantages of using Ignite UI is that it’s designed for modern Web, and it is designed to fit and adjust for all screen sizes and works with all browsers and devices without doing any manual work. Learn more and download Ignite UI for Angular here.

  1. Install Ignite UI for Angular
  2. Create Angular Project with Ignite UI
  3. Import the IgxNavbarModule
  4. Adding the igx-navbar
  5. Adding fonts in “index.html”
  6. Adding Icons in navbar
  7. Adding back button in navbar
  8. Using Navbar and NavigationDrawer together
  9. Conclusion

Install Ignite UI for Angular

You should have node and npm installed on your machine. If you are new to npm, check out the npm documentation here.

Once npm is installed, open the command prompt and run

“npm install -g igniteui-cli”

This will install Ignite UI on your machine and it is ready to use.

Create an Angular project with Ignite UI

To create new project, run the below command:

“ng new nav-bar-demo”

To get in to the project, type:

“cd nav-bar-demo”

Now, it’s time to install the Ignite UI for the current project.

Run the following command,

“npm install igniteui-angular”

Let’s add the required Ignite UI styles and the HammerJs library in the angular-cli.json.

Open the “angular.json” and add below code snippet as shown Listing 1.

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

Listing 1.

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

Run the following command on command prompt to run the Angular application.

ng serve --open

Application will run using the following address and port http://localhost:4200.

Import IgxNavbarModule

Open the “app.module.ts” file and import the igxNavBarModule in import section of @NgModule

  1. import { IgxNavbarModule, IgxIconModule } from 'igniteui-angular';  
  2. @NgModule({  
  3. imports: [  
  4.    IgxNavbarModule,  
  5. ],  
  6. providers: [],  
  7.    bootstrap: [AppComponent]  
  8. })  
  9. export class AppModule { } 

Listing 2.

Add nav-bar component

Let’s add the new component named nav-bar. To do that, run the below command on command prompt.

ng generate component components/nav-bar

Listing 3.

Nav Bar in Angular
Figure 1.

Now add the <app-nav-bar></app-nav-bar> in app.component.html file.

Adding igx-navbar

Let’s add the igx-navbar directive in “nav-bar.component.html” page as showing in listing 4.

  1. <article class="sample-column">  
  2.     <div class="navbar-sample">  
  3.         <igx-navbar title="Navbar Sample" actionButtonIcon="menu" [isActionButtonVisible]="true"> </igx-navbar>  
  4.     </div>  
  5. </article>  

Listing 4.

Adding fonts in “index.html”

  1. <!doctype html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>NavbarDemo</title>  
  7.     <base href="/">  
  8.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  9.     <link rel="icon" type="image/x-icon" href="favicon.ico">  
  10.     <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">  
  11.     <link href="https://fonts.googleapis.com/css?family=Titillium+Web:300,400,600,700" rel="stylesheet">  
  12.     <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">  
  13. </head>  
  14.   
  15. <body>  
  16.     <app-root></app-root>  
  17. </body>  
  18.   
  19. </html>  

Listing 5.

Now, run the application using “ng server --open”

Nav Bar in Angular
Figure 2.

Adding Icons in navbar

Let’s add the icons in navbar to make it more appealing. Add the “IgxIconModule” in app.module.ts file as showing in Listing 6.

  1. import { IgxNavbarModule, IgxIconModule } from 'igniteui-angular';  
  2. @NgModule({  
  3.    imports: [  
  4.    IgxNavbarModule,  
  5.    IgxIconModule  
  6. ],  
  7. export class AppModule { }  

Listing 6.

Now, open the navbar.component.html and add the following html for icon,

  1. <article class="sample-column">  
  2.     <div class="navbar-sample">  
  3.         <igx-navbar title="Navbar Sample" actionButtonIcon="menu" [isActionButtonVisible]="true">  
  4.             <igx-icon name="search"></igx-icon>  
  5.             <igx-icon name="favorite"></igx-icon>  
  6.             <igx-icon name="more_vert"></igx-icon>  
  7.         </igx-navbar>  
  8.     </div>  
  9. </article>  

Listing 7.

Now, run the application to view the output,

Nav Bar in Angular
Figure 3.

Adding back button in navbar

Let’s add the back button in the navbar. To do that, we need to add a few properties in the igx-navbar

  1. actionButtonIcon="arrow_back"  
  2. [isActionButtonVisible]="canGoBack()"  
  3. (onAction)="navigateBack()" 

Follow the below the listing 8 for adding the above properties.

  1. <article class="sample-column">  
  2.     <div class="navbar-sample">  
  3.         <igx-navbar title="Navbar Sample" actionButtonIcon="arrow_back" [isActionButtonVisible]="canGoBack()" (onAction)="navigateBack()">  
  4.             <igx-icon name="search"></igx-icon>  
  5.             <igx-icon name="favorite"></igx-icon>  
  6.             <igx-icon name="more_vert"></igx-icon>  
  7.         </igx-navbar>  
  8.     </div>  
  9. </article> 

Listing 8.

Now we have added the back button, let’s add the back navigation functionality to the navbar.

Open the app.component.ts file for adding the below code shown in listing 8. We need to import the location package to add the go back functionality.

  1. import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';  
  2. @NgModule({  
  3. providers: [Location, {provide: LocationStrategy, useClass:                                                                                                                                  PathLocationStrategy}],  
  4.    bootstrap: [AppComponent]  
  5. })  
  6. export class AppModule { }  

Listing 9.

Also, add the below code the nav.component.ts for clicking the go back button.

  1. import { Component, OnInit } from '@angular/core';  
  2. import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';  
  3. const CURRENT_VIEW = 'Ignite UI for Angular';  
  4. @Component({  
  5.    selector: 'app-nav-bar',  
  6.    templateUrl: './nav-bar.component.html',  
  7.    styleUrls: ['./nav-bar.component.css']  
  8. })  
  9. export class NavBarComponent implements OnInit {  
  10. public currentView: string;  
  11. constructor(private _location: Location) { }  
  12. public ngOnInit() {  
  13.    this.currentView = CURRENT_VIEW;  
  14. }  
  15. public navigateBack() {  
  16.    this._location.back();  
  17. }  
  18. public canGoBack() {  
  19.    return window.history.length > 0;  
  20.    }  
  21. }  

Listing 10.

Now, run the application

Nav Bar in Angular
Figure 4.

Using Navbar and NavigationDrawer together

Let’s extend this sample more and add the nav bar and navigation together in this application.

Create three new component,

  • ng generate component components/dashboard
  • ng generate component components/settings
  • ng generate component components/about

Nav Bar in Angular
Figure 5.

Now, add the new file app-routing.module.ts.

  1. import { NgModule } from '@angular/core';  
  2. import { Routes, RouterModule } from '@angular/router';  
  3. import { DashboardComponent } from './components/dashboard/dashboard.component';  
  4. import { SettingsComponent } from './components/settings/settings.component';  
  5. import { AboutComponent } from './components/about/about.component';  
  6.   
  7. const routes: Routes = [  
  8. { path: '', component: DashboardComponent },  
  9. { path: 'dashboard', component: DashboardComponent },  
  10. { path: 'settings', component: SettingsComponent },  
  11. { path: 'about', component: AboutComponent }  
  12. ];  
  13. @NgModule({  
  14.    imports: [RouterModule.forRoot(routes)],  
  15.    exports: [RouterModule]  
  16. })  
  17. export class AppRoutingModule { } 

Listing 11.

Add the AppRoutingModule in the app.module.ts file. Also, add the “IgxNavigationDrawerModule” in same file as shown in listing 12.

  1. import {  
  2.     IgxNavbarModule,  
  3.     IgxIconModule,  
  4.     IgxNavigationDrawerModule  
  5. } from 'igniteui-angular';  
  6. import {  
  7.     AppRoutingModule  
  8. } from './app-routing.module';  
  9. @NgModule({  
  10.             imports: [  
  11.                 AppRoutingModule,  
  12.                 IgxNavigationDrawerModule  
  13.             ]  
  14.             export class AppModule {} 

Listing 12.

Now, open app.component.html file and replace existing html with the following HTML in Listing 13.

  1. <igx-navbar [title]="title" actionButtonIcon="menu" (onAction)="drawer.toggle()">  
  2.     <igx-icon name="add" (click)="onClickAdd()"></igx-icon>  
  3. </igx-navbar>  
  4. <igx-nav-drawer #drawer [pinThreshold]="false" width="280px">  
  5.     <div class="ig-drawer-content">  
  6.         <ng-template igxDrawer>  
  7.             <nav class="nav"> <span class="nav-item header">menus</span> <span class="nav-item" igxDrawerItem igxRipple [routerLinkActive]="'active'" routerLink="dashboard" (click)="drawer.close();"> <igx-icon fontSet="material" name="home"></igx-icon> <span>Dashboard</span> </span> <span class="nav-item" igxDrawerItem igxRipple [routerLinkActive]="'active'" routerLink="settings" (click)="drawer.close();"> <igx-icon fontSet="material" name="settings"></igx-icon> <span>Setting</span> </span> <span class="nav-item" igxDrawerItem igxRipple [routerLinkActive]="'active'" routerLink="about" (click)="drawer.close();"> <igx-icon fontSet="material" name="person"></igx-icon> <span>About</span> </span> </nav>  
  8.         </ng-template>  
  9.     </div>  
  10. </igx-nav-drawer>  
  11. <router-outlet></router-outlet> 

Listing 13.

Now, run the app and open it in a browser (mobile mode) using F12 in Chrome.

Nav Bar in Angular
Figure 6.

Nav Bar in Angular

Conclusion

Navbar is a very useful component of Ignite UI for Angular that allows developers to add a navigation header to a website or page. In this article, we saw how to add a navbar to a webpage and how to build its functionality to navigate through the website. It is responsive and works flawlessly on any device of any size.

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


Similar Articles