I am building my first Angular 4 app, using ng-bootstrap (Bootstrap 4 for Angular) - not using regular Boostrap
The ng-bootstrap website (https://ng-bootstrap.github.io/#/components/accordion/api) does not list a component for a navbar (as it did in Bootstrap 3 or regular bootstrap 4). Does that mean I have to build a menu out of dropdowns/buttons? Or I have to mix regular Bootstrap 4 with ng-bootstrap?
I tried creating this menu in the html but it won't render in the browser (just a brand "MyWebSiteName" and little grey box on my screen, no nav)
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">MyWebSiteName</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Page 1-1</a></li>
<li><a href="#">Page 1-2</a></li>
<li><a href="#">Page 1-3</a></li>
</ul>
</li>
<li><a href="#">Page 2</a></li>
<li><a href="#">Page 3</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</div>
</nav>
I something wrong with this HTML ? Or does someone have an example of a Navbar that works with ng-bootstrap?
As there is no navigation component you'd need to use a combination of the collapse functionality ng-bootstrap's ngbCollapse component as well as the dropdown functionality from the NgbDropdown component.
You'll need to bind an instance of ngbCollapse
to the div.collapse.navbar-collapse
and a boolean property on your component class. ng-bootstrap doesn't have any use for data-*
so you can remove attributes such as data-toggle="collapse"
.
You control the open/close state of the collapse menu via a property on your component class that gets toggled true
/false
through something like a click event. In this example is toggle via a (click)
event handler on the button.navbag-toggler
executing a method on the component toggleMenu()
which simply inverts the value of boolean property isCollapsed
via the !
operator.
For the menu item dropdown menu, you'd use the NgbDropdown component. You'd apply attributes ngbDropdown
and ngbDropdownToggle
to the container element and toggle element respectively.
<div class="nav-item dropdown" ngbDropdown>
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" ngbDropdownToggle>
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="dropdownBasic1">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
In terms of styling for Bootstrap 4 navbar, you'll need to use the following classes:
Navbars require a wrapping .navbar with .navbar-toggleable-* for responsive collapsing and color scheme classes.
Also elements such as button.navbar-toggle
are now button.navbar-toggler
with an "r" in Bootstrap 4. You use classes navbar-inverse
and bg-inverse
for the standard inverse navbar.
HTML:
<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" aria-controls="appNavigation" [attr.aria-expanded]="!isCollapsed" aria-label="Toggle navigation" (click)="toggleMenu()">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">My App</a>
<div class="collapse navbar-collapse" id="appNavigation" [ngbCollapse]="isCollapsed">
<div class="navbar-nav mr-auto">
<a class="nav-item nav-link" routerLink="" routerLinkActive="active">Home</a>
<a class="nav-item nav-link" routerLink="/about" routerLinkActive="active">About</a>
<div class="nav-item dropdown" ngbDropdown>
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" ngbDropdownToggle>
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="dropdownBasic1">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
</div>
</div>
</nav>
TS:
export class NavigationComponent {
isCollapsed = true;
constructor() {}
toggleMenu() {
this.isCollapsed = !this.isCollapsed;
}
}
Here is a plunker demonstrating the functionality and styling in action.
Instead of Using ng-Bootstrap try using Angular for the same.
Small Example
Template
<nav class="navbar navbar-toggleable-md navbar-light bg-faded navbar-fixed-top">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation" (click)="isActive = !isActive"> // add this for the responsive button
<span class="navbar-toggler-icon"></span>
</button>
<a routerLink="/home"><img src="angular.png" class = "myImage" alt="Image"/></a>
<div class="collapse navbar-collapse" id="navbarNavDropdown" [ngClass]="{show : isActive}">
<ul class="navbar-nav">
<li class="nav-item dropdown" [routerLinkActive]="['active']" appDropdown> // add this directive for the dropdown
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Basic Concepts
</a>
.....
Directive
import {Directive, HostListener, HostBinding} from '@angular/core';
@Directive({
selector: '[appDropdown]'
})
export class DropdownDirective {
private isOpen:boolean = false;
@HostBinding('class.open') get opened(){
return this.isOpen;
}
constructor() { }
@HostListener('click')open(){
this.isOpen = true;
}
@HostListener('mouseleave')close(){
this.isOpen = false;
}
Working example of the Same the navbar on top is made using bootstrap and Angular directives Link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With