17

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.

Braste84
  • 173
  • 1
  • 1
  • 5

7 Answers7

50

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;
     }
}
Ronny Brendel
  • 4,777
  • 5
  • 35
  • 55
jmiguel77
  • 824
  • 9
  • 19
  • {'navbar-collapse': true} should be placed in class not ngClass – tsu Nov 27 '19 at 22:43
  • This may work but this is not the right solution. You need to include the bootstrap script in the angular.json file in the right order. Something like this, "scripts": [ "node_modules/pace-js/pace.min.js", "node_modules/jquery/dist/jquery.js", "node_modules/datatables.net/js/jquery.dataTables.js", "node_modules/bootstrap/dist/js/bootstrap.min.js" ] – Sanish Joseph Jan 06 '20 at 14:09
  • any way to get this working with animation? – Ethan SK Dec 25 '20 at 20:03
9

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.

Karthik H
  • 1,267
  • 12
  • 13
4

As what was mentioned in the comments and the top answer by jmiguel77

Just wanted to share that I also found a similar way of doing this on ng-bootstrap Github docs https://ng-bootstrap.github.io/#/components/collapse/examples

I used [ngbCollapse]="isCollapsed"instead. Both way come up with the same result from what I saw.

Here is my code:

//app-navbar.component.html


   <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
  <a class="navbar-brand" href="#">{{title}}</a>
  <button class="navbar-toggler" type="button" (click)="toggleCollapsed()" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon" ngToggler></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarsExampleDefault"  [ngbCollapse]="isCollapsed" >
    <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>
      <li class="nav-item dropdown" ngbDropdown>
        <a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" ngbDropdownToggle>Dropdown</a>
        <div class="dropdown-menu" aria-labelledby="dropdown01" ngbDropdownMenu>
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

//app-navbar.component.ts

import { Component, OnInit, Input } from '@angular/core';




@Component({
  selector: 'app-navbar',
  templateUrl: './app-navbar.component.html',
  styleUrls: ['./app-navbar.component.css']
})
export class AppNavbarComponent implements OnInit {
  @Input() title: String;
  isCollapsed = true;
  toggleCollapsed(): void {
    this.isCollapsed = !this.isCollapsed;
  }

  constructor() { }



  ngOnInit() {
  }



}
1
#you can use click method in your html and use router navigate in your type script.

#In HTML File
     <a class="nav-link" (click)="onMenuClick('profile')">Profile </a>
#In Type Script file

 onMenuClick(path) {
    $('.navbar-toggler').click();
    this.router.navigate(['/' + path]);

  }
0

Add "./node_modules/jquery/dist/jquery.js", in Angular.json file under script tag and it will work.

0

I thought I could share what worked for me.

I've got a couple of requirements you might consider;

  • Angular 8
  • Bootstrap 4.5

...inside my html template [header.component.html]


<nav id="mainNavbar" class="navbar navbar-expand-sm navbar-light bg-light">
  <div class="container-fluid">
    <a routerLink="/" class="navbar-brand">APP_NAME</a>
    <button
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
      (click)="collapsed = !this.collapsed"
    >
      <span class="navbar-toggler-icon"></span>
    </button>
    <div
      id="navbarSupportedContent"
      [ngClass]="{ collapse: collapsed, 'navbar-collapse': true }"
    >
      <!-- right nav menus -->
      <ul class="navbar-nav ml-auto">
        <li routerLinkActive="active" class="nav-item">
          <a routerLink="/LINK_1" class="nav-link"> lINK_1 </a>
        </li>
        <li routerLinkActive="active" class="nav-item">
          <a routerLink="/LINK_2" class="nav-link"> LINK_2 </a>
        </li>
      </ul>
    </div>
  </div>
</nav>

...inside my ts component [header.component.ts]

import { Component, OnInit} from "@angular/core";

@Component({
  selector: "app-header",
  templateUrl: "./header.component.html",
  styleUrls: ["./header.component.css"],
})
export class HeaderComponent implements OnInit {

  collapsed = true;

  constructor() {}

  ngOnInit() {}
}

I hope this helps. Feedbacks and suggestions are appreciated. :)

John Erbynn
  • 355
  • 1
  • 4
  • 14
0

I had the same problem with Angular 10, Bootstrap 4.5. After 2 days of checking all possibilites with installing and providing all necessary dependencies I've followed instructions from link provided by Wyatt Lansdale (https://ng-bootstrap.github.io/#/components/collapse/examples).

As long as I had hamburger icon from Bootstrap docs (< span class="navbar-toggler-icon" >< /span >) there was still no difference, but once I change it for unicode character for the hamburger icon (& # 9 7 7 6 ; without spaces) problem is solved! Eventually works:)