ngx-bootstrap icon indicating copy to clipboard operation
ngx-bootstrap copied to clipboard

Dropdown not working

Open GeRoHi opened this issue 9 years ago • 17 comments

Hello, I'm trying to make a standard navbar works (https://getbootstrap.com/components/#navbar) I have installed bootstrap-ngx with angular-cli.

my app.module.ts import { AlertModule } from 'ngx-bootstrap'; import { CollapseModule } from 'ngx-bootstrap'; import { BsDropdownModule } from 'ngx-bootstrap'; ... imports: [ AlertModule.forRoot(), CollapseModule.forRoot(), BsDropdownModule.forRoot() ],

Still, dropdown not showing, and no error in console.. what am I missing ?

Thank you !

GeRoHi avatar May 31 '17 14:05 GeRoHi

Have you tried adding the code from demo to your template? I just don't see any actual initialization of dropdown in the piece of code you've provided Try to add the following code to your template

<div class="btn-group" dropdown>
  <button dropdownToggle type="button" class="btn btn-primary dropdown-toggle">
    Button dropdown <span class="caret"></span>
  </button>
  <ul *dropdownMenu class="dropdown-menu" role="menu">
    <li role="menuitem"><a class="dropdown-item" href="#">Action</a></li>
    <li role="menuitem"><a class="dropdown-item" href="#">Another action</a></li>
    <li role="menuitem"><a class="dropdown-item" href="#">Something else here</a></li>
    <li class="divider dropdown-divider"></li>
    <li role="menuitem"><a class="dropdown-item" href="#">Separated link</a>
    </li>
  </ul>
</div>

IlyaSurmay avatar May 31 '17 18:05 IlyaSurmay

Actually have this is my navbar <li class="dropdown"> <a dropdownToggle href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a> <ul *dropdownMenu class="dropdown-menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li role="separator" class="divider"></li> <li><a href="#">Separated link</a></li> <li role="separator" class="divider"></li> <li><a href="#">One more separated link</a></li> </ul> </li> Added *dropdownMenu directive to ul element and dropdownToggle to a, still not working. Your dropdown however is working fine, thanks !

GeRoHi avatar May 31 '17 18:05 GeRoHi

Hi, i confirm the dropdown doesn't works with "a" tag, but works well with button. regards

cyberbobjr avatar Jun 01 '17 11:06 cyberbobjr

Well, why is it defined with an a element on the official bootstrap website then ?

<li class="dropdown"> 
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a> 
  <ul class="dropdown-menu"> 
    <li><a href="#">Action</a></li>
    <li><a href="#">Another action</a></li> 
    <li><a href="#">Something else here</a></li> 
    <li role="separator" class="divider"></li> 
    <li><a href="#">Separated link</a></li> 
    <li role="separator" class="divider"></li> 
    <li><a href="#">One more separated link</a></li> 
  </ul> 
</li>

Is this a restriction due to the use of bootstrap-ngx ?

GeRoHi avatar Jun 01 '17 12:06 GeRoHi

+1 same problem with a dropdown with a tag

Axel3232 avatar Jun 02 '17 19:06 Axel3232

Dropdown doesn't prevent default behavior of a tag on click Please use code from ngx-bootstrap demo pages, html you have provided is not valid

valorkin avatar Jun 02 '17 20:06 valorkin

So, maybe we can we overwrite onclick behavior of the a tag ? (click)='dropdown.toggle()' ?

GeRoHi avatar Jun 02 '17 20:06 GeRoHi

It depends on what you need, samples works fine with a tag. So I am not sure, what is not working http://valor-software.com/ngx-bootstrap/#/dropdowns#single-button

valorkin avatar Jun 02 '17 21:06 valorkin

This worked for me using a navbar.

<li class="dropdown" dropdown>
    <a href dropdownToggle (click)="false" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
    <ul *dropdownMenu class="dropdown-menu">
        <li role="menuitem"><a href="#">Action</a></li>
        <li role="menuitem"><a href="#">Another action</a></li>
        <li role="menuitem"><a href="#">Something else here</a></li>
        <li role="separator" class="divider" dropdown-divider></li>
        <li role="menuitem"><a href="#">Separated link</a></li>
    </ul>
</li>

joeangry avatar Jun 04 '17 14:06 joeangry

Mine was not working due to bootstrap3 vs bootstrap4 confusion. When using bootstrap4, definitely verify all the steps shown here: https://github.com/valor-software/ngx-bootstrap/blob/development/docs/getting-started/bootstrap4.md When these steps are not completed, ngx-bootstrap will not properly change the classes of the btn-group. Bootstrap4 needs to see a "show" class added, but ngx-bootstrap was only adding an "open" class. After applying all the bootstrap4 steps it's working. Both classes are added: "open show"

pandachris avatar Jul 13 '17 09:07 pandachris

@joeangry thanks it worked for me.

lucifer-AM avatar Aug 18 '17 05:08 lucifer-AM

If you are using a navbar and an "a" element, (click)=false is the solution. Thanks @joeangry.

cwienands1 avatar Oct 28 '17 03:10 cwienands1

If you find this old issue in 2019 because the dropdown is not working on an <a> tag but it is working on a <button> , try @joeangry 's solution verbatim. In my case (using Bootstrap 3) I initially copied and pasted the sample code from the documentation into my component, and it still didn't work.

I found I had to add the "dropdown" class to the outermost , like @joeangry's solution has, but the sample code does not, to make the dropdown appear.

masonmark avatar Aug 01 '19 07:08 masonmark

I tried changing the version of ngx Bootstrap to 4.0.1 and it worked for me Command I executed : npm install [email protected] --save

ManideepInnamuri avatar Apr 29 '20 11:04 ManideepInnamuri

This worked for me using a navbar.

<li class="dropdown" dropdown>
    <a href dropdownToggle (click)="false" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
    <ul *dropdownMenu class="dropdown-menu">
        <li role="menuitem"><a href="#">Action</a></li>
        <li role="menuitem"><a href="#">Another action</a></li>
        <li role="menuitem"><a href="#">Something else here</a></li>
        <li role="separator" class="divider" dropdown-divider></li>
        <li role="menuitem"><a href="#">Separated link</a></li>
    </ul>
</li>

This code works, I have tried with all the examples in the official ngx bootstrap and no one works, so thanks @joeangry for your help.

demesajorge avatar May 21 '20 15:05 demesajorge

The "Trigger by Tag " code in the official ngx bootstrap site (https://valor-software.com/ngx-bootstrap/#/dropdowns#anchor-trigger) is still not working @2020. Guys, please add class="dropdown" into <span> to fix it while ngx team don´t release it.

The example on link still doesn't work for me if I switch from <span dropdown> to <span class="dropdown">

VjeraTurk avatar Dec 02 '20 13:12 VjeraTurk