I am using ng-bootstrap in my angular 4 project:
<navbar class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<button aria-controls="navbarsDefault" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler navbar-toggler-right" data-target="#navbarsDefault" data-toggle="collapse" type="button">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarsDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link active" routerlink="/dashboard" routerlinkactive="active" ng-reflect-router-link="/dashboard" ng-reflect-router-link-active="active" href="/dashboard">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link" routerlink="/program" routerlinkactive="active" ng-reflect-router-link="/program" ng-reflect-router-link-active="active" href="/program">Programm</a>
</li>
</ul>
</div>
</navbar>
The navbar is there, it is responsive, it seems to work fine. But the navbar-toggler button does not toggle the navbar, when it is collapsed and i can't find an error.
I ended up doing something like this:
In the html template i have this code:
<nav id="mainNavbar" class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation"
(click)="toggleCollapsed()">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbarSupportedContent" [ngClass]="{'collapse': collapsed, 'navbar-collapse': true}">
......
</div>
</nav>
In the component I have this
export class NavComponent {
collapsed = true;
toggleCollapsed(): void {
this.collapsed = !this.collapsed;
}
}
Its very simple and quite straight forward,
in C:\Niagara\Vega\ncPortal\.angular-cli.json
under scripts, add this line, or point to bootstrap.js file from your node_modules,
"../node_modules/bootstrap/dist/js/bootstrap.js"
and rebuild your app, now, not only the toggle, but all the other actions too will work as expected.
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