Add a Responsive Bootstrap Navigation Bar in Angular 9 mobile view

How to Add a Responsive Bootstrap Navigation Bar in Angular 9 ?

This tutorial guides you on how to add a responsive bootstrap navigation bar in Angular 9 application. Let’s see how to use bootstrap navbar in Angular to build a responsive navigation menu bar.

Add a Responsive Bootstrap Navigation Bar in Angular 9

First, you need to install bootstrap correctly. Please check the following link to know onΒ How to Install Bootstrap Correctly

Angular bootstrap navbar is a simple wrapper that we are going to use for building navigation bar and other elements in our navigation header. Let’s say you had created header angular component as shown. Please check the following link to know onΒ How to create a new component with angular CLI.

πŸ“‚src
| β€” πŸ“‚ app
| β€” | β€” πŸ“‚ header
| β€” | β€” | β€” πŸ“œ header.component.html
| β€” | β€” | β€” πŸ“œ header.component.ts
| β€” | β€” πŸ“‚ recipes
| β€” | β€” | β€” πŸ“‚ recipes
| β€” | β€” | β€” πŸ“œ recipes.component.html
| β€” | β€” | β€” πŸ“œ recipes.component.ts
| β€” | β€” | β€” πŸ“œ recipes.component.css
| β€” | β€” πŸ“‚ app.component.html
| β€” | β€” πŸ“‚ app.component.ts
| β€” | β€” πŸ“‚ app.component.css
| β€” | β€” πŸ“‚ app.component.spec.ts

Then, you need to have the following lines of code in your header component template file. We have used wrapping nav element and on this element we have added navbar bootstrap classes. This will give the default bootstrap header.

header.component.html

<nav class="navbar navbar-default"> 
    <div class="container-fluid">
        <div class="navbar-header">
                <button type="button" class="navbar-toggle" (click)="collapsed = !collapsed">
                  <span class="icon-bar" *ngFor="let iconBar of [1, 2, 3]"></span>
                </button>
                <a routerLink="/" class="navbar-brand">Myrecipes.com</a>
        </div>
        <div class="navbar-collapse" [class.collapse]="collapsed" (window:resize)="collapsed = true">
            <ul class="nav navbar-nav">
                <li><a href="#">Recipes</a></li>
                <li><a href="#">Contact Us</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Settings</a></li>
            </ul>            
        </div>
    </div>
</nav>

And your header component typescript file should look like below.

header.component.ts

import { Component } from '@angular/core';


@Component({
   selector:'app-header',
   templateUrl: './header.component.html'
})

export class HeaderComponent {
   collapsed = true;
}

Finally, your app.component.html file should have the app-header selectors tag as shown.

<app-header></app-header>

That’s it you have created a header component and addedΒ a responsive bootstrap navigation bar in Angular 9 application. When you try to run the above code you should see the following output as shown in the screenshot below.

Add a Responsive Bootstrap Navigation Bar in Angular 9

Responsive Bootstrap Navigation Bar : Angular

And you could also check the responsiveness using Google chrome “Inspect” tool to see how it will look on mobile device. For example, the below screenshot shows how it looks on iPhone X device.

Add a Responsive Bootstrap Navigation Bar in Angular 9 mobile view

Hence, it is working. You could see Hamburger Menu in the mobile view and you can access all your menu items or links even in smaller screens.

Note, the following lines of code had helped you to see Hamburger menu in the smaller screens.

header.component.html

<div class="navbar-header">
   <button type="button" class="navbar-toggle" (click)="collapsed = !collapsed">
       <span class="icon-bar" *ngFor="let iconBar of [1, 2, 3, 4]"></span>
   </button>
   <a routerLink="/" class="navbar-brand">Myrecipes.com</a>
</div>
<div class="navbar-collapse" [class.collapse]="collapsed" (window:resize)="collapsed = true">

And the following property declaration “collapsed” in the component class.

header.component.ts

export class HeaderComponent {
   collapsed = true;
}

That’s all. You had learnt howΒ to add a responsive bootstrap navigation bar in Angular 9 application.

Hope it helped πŸ™‚

Also See:

References:

guest
0 Comments
Inline Feedbacks
View all comments