Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap 4 in Angular 2 dropdown not working

I have followed the following to install bootstrap 4 into my Angular 2 project: Accepted Answer, following the first 1,2,3 and 4 steps

However when I add the following HTML to my header component:

<nav class="navbar-dark bg-inverse">
<div class="container">
    <a href="#" class="navbar-brand"></a>
    <ul class="nav navbar-nav float-xs-right">
        <li class="nav-item dropdown">
            <a href="#" class="nav-link dropdown-toggle" id="nav-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">[email protected]</a>
            <div class="dropdown-menu" aria-labelledby="nav-dropdown">
                <a href="#" class="dropdown-item">Sign Out</a>
            </div>
        </li>
    </ul>
</div>

As you can see its a dropdown basically, when I click on the dropdown the page refreshes, instead it doesn't display the 'sign out' option.

This is my angular-cli.json styles section:

"styles": [
  "styles.css",
  "../node_modules/bootstrap/dist/css/bootstrap.min.css"
],    

And inside my Angular 2 module:

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

I then import NgbModule into the imports section.

I've clearly missed something, can someone shed any light into what it maybe exactly?

like image 595
Code Ratchet Avatar asked Dec 24 '16 23:12

Code Ratchet


People also ask

Why drop down menu is not working in angular?

You have to make sure that popper. js is included and loaded in your Angular app because that is required for all things that pop up or drop down in Bootstrap 4. It works!!!

Why is bootstrap drop down not working?

Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.

Why is dropdown not showing?

In your case you have overflow:hidden on . row which holds the menu and therefore the dropdown menu will never show because it overflows the container. For the element called . row that holds the menu you will need to use another clearing mechanism instead of overflow:hidden.

What is drop down in bootstrap?

Dropdowns are toggleable, contextual overlays for displaying lists of links and more. They're made interactive with the included Bootstrap dropdown JavaScript plugin. They're toggled by clicking, not by hovering; this is an intentional design decision. Dropdowns are built on a third party library, Popper.


2 Answers

Like Andrien's said, you can simplify the code like this.

constructor(private _el: ElementRef) { }

@HostBinding('class.show') isOpen = false;

@HostListener('click') toogleOpen() {
    this.isOpen = !this.isOpen;
    this._el.nativeElement.querySelector('.dropdown-menu').classList.toggle('show')
}
like image 75
dpolicastro Avatar answered Oct 18 '22 21:10

dpolicastro


  1. Please install ng-bootstrap from this link Getting Started with the following command:

    npm `install --save @ng-bootstrap/ng-bootstrap`
    
  2. Import it on app.module.ts like

    import `{NgbModule} from '@ng-bootstrap/ng-bootstrap';` 
    
  3. Import on

    imports:[
       NgbModule.forRoot(),
    ]
    
  4. Add ngbDropdown on dropdown

  5. Add ngbDropdownToggle on dropdown Toggle DOM

  6. Add ngbDropdownMenu on dropdown menu DOM

                <li ngbDropdown  class="nav-item dropdown" >
                    <a ngbDropdownToggle class="nav-link dropdown-toggle"  href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                     Manage
                    </a>
                    <div ngbDropdownMenu  class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                      <a class="dropdown-item" href="#">Save Data</a>
                      <a class="dropdown-item" href="#">Fetch Data</a>
    
                    </div>
                  </li>
              </ul>
    
like image 40
Shahin Al Kabir Mitul Avatar answered Oct 18 '22 19:10

Shahin Al Kabir Mitul