Bootstrap Navbar not working correctly: AngularCLI
The code below shows how to implement the navbar in Angular with ng-bootstrap and Bootstrap 4. You can see it at work in this plunker.
- The
ng-bootstrap
library is developped for Angular, and does not require jQuery - Using Bootstrap 4, you should replace
navbar-toggleable-md
withnavbar-expand-md
- The
ngbCollapse
directive is used to show/hide the dropdown menu
<nav class="navbar navbar-expand-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" (click)="isNavbarCollapsed = !isNavbarCollapsed" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div [ngbCollapse]="isNavbarCollapsed" class="navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
navbar.component.html:
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" (click)="toggleNavbarCollapsing()">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a routerLink="/" class="navbar-brand">Navbar</a>
</div>
<div class="navbar-collapse" [class.collapse]="navbarCollapsed">
...
navbar.component.ts:
navbarCollapsed = true;
toggleNavbarCollapsing() {
this.navbarCollapsed = !this.navbarCollapsed;
}
There are some known issues in bootstrap 4 especially in navbar implemenation
Hence uou have to install bootstrap [email protected]
if you are using npm then the following libraries is best recommonded
npm install [email protected] --save
npm install [email protected] tether jquery --save
In .angular-cli.json file add the dependencies
"styles":[
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.css"
],
"scripts":[
"../node_modules/jquery/dist/jquery.js",
"../node_modules/tether/dist/js/tether.js",
"../node_modules/bootstrap/dist/js/bootstrap.js"
],
Use the following HTML
<nav class="navbarnavbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-togglecollapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Togglenavigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Angular5Website</a>
</div>
<div id="navbar" class="collapsenavbar-collapse">
<ul class="navnavbar-nav">
<li class="active"> <a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--/.nav-collapse-->
</div>
</nav>