Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navbar toggle not working with Angular 4 / ng-bootstrap

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.

like image 674
Braste84 Avatar asked Sep 21 '17 15:09

Braste84


Video Answer


2 Answers

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;
     }
}
like image 96
jmiguel77 Avatar answered Sep 19 '22 19:09

jmiguel77


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.

like image 34
Karthik H Avatar answered Sep 21 '22 19:09

Karthik H