dropdown toggle issue in navbar with ng-bootstrap (4) angular 2 (cli)

11,968

Solution 1

I got it working like this:

<nav class="navbar navbar-light bg-faded">
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav navbar-nav">
    <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" href="#">Link</a>
    </li>
    <li class="nav-item dropdown">
      <div ngbDropdown class="d-inline-block">
        <button class="btn btn-outline-primary" id="dropdownMenu1" ngbDropdownToggle>Toggle dropdown</button>
        <div ngbDropdownMenu class="dropdown-menu" aria-labelledby="dropdownMenu1">
          <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>
    </li>
  </ul>
  <form class="form-inline float-xs-right">
    <input class="form-control" type="text" placeholder="Search">
    <button class="btn btn-outline-success" type="submit">Search</button>
  </form>
</nav>

As pointed above in the comment (link)

Solution 2

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}">
......
</nav>

In the component i have this

 export class NavComponent {
      collapsed = true;
      toggleCollapsed(): void {
        this.collapsed = !this.collapsed;
      }
 }

Solution 3

modify your drown down html parts to this:

<li class="nav-item dropdown">
  <div ngbDropdown class="d-inline-block">
    <button class="btn btn-outline-primary" id="dropdownMenu1" 
      **ngbDropdownToggle**>use Attrib ngbDropdownToggle</button>

    <!-- use  ngbDropdownMenu -->
    <div **ngbDropdownMenu** class="dropdown-menu" aria-labelledby="dropdownMenu1">
      <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>
</li>    
Share:
11,968

Related videos on Youtube

Tanweer
Author by

Tanweer

I am a senior full-stack software engineer with a focus on building intuitive and effective web applications in Elixir(Erlang), Nodejs, Ruby, Golang and Angular/react/Vue using best practices and common design patterns. I am a problem solver, architect and have more than 7 years of industry Experience. I can inter-convert nodejs, elixir/erlang, RoR codebase. I have a very firm grasp in TDD, deployments and API development. I have a firm grasp in working with iot devices(via mqtt). I provide High-quality, maintainable backend and frontend solutions. I can work on creating complex web applications. I can create single-page web applications (SPA) using Angular/Vue/React. I offer services in 3rd party API integrations. Payment gateways integration (Stripe, PayPal etc). Graphql IoT/IoT core Backend + APIS Able to work as part of a team or solely. I can also lead/manage a team for you. I have worked on enterprise level applications and projects with millions of users. I have good experience with indexing, complex queries, caching, common security holes and dealing with big data.

Updated on September 24, 2022

Comments

  • Tanweer
    Tanweer about 2 months

    I followed the instructions described here and css is working perfectly. For the dropdown toggle I used this code

    <nav class="navbar navbar-light bg-faded">
      <a class="navbar-brand" href="#">Navbar</a>
      <ul class="nav navbar-nav">
        <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" href="#">Link</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="supportedContentDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
          <div class="dropdown-menu" aria-labelledby="supportedContentDropdown">
            <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 float-xs-right">
        <input class="form-control" type="text" placeholder="Search">
        <button class="btn btn-outline-success" type="submit">Search</button>
      </form>
    </nav>
    

    But the dropdown toggle is not working, although I include NgbModule for Root. Please me know if I had to include something else in my component providers array or anything else. Thanks in advance