No provider for NgbDropdown with Angular Bootstrap
Asked Answered
S

3

28

I am attempting to use the NbgDropdown in an app and I get the following error:

NullInjectorError: No provider for NgbDropdown!

My app.module file looks as follows:

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

NgModule({
    declarations: [
        ...
    ],
    imports: [
        ...
        NgbModule.forRoot()
    ],
    providers: [
        ...
    ],
    bootstrap: [AppComponent]
})

From other answers, this looks like what should be required

I saw the following terse comment that might help, but it is not meaningful to me:

https://mcmap.net/q/503209/-angular2-ng-bootstrap-no-provider-error

Sabo answered 21/2, 2019 at 20:50 Comment(5)
Check the official example here: stackblitz.com/angular/kopbjmkbmabn?file=app%2Fapp.module.tsHaematinic
So, is the trick that the menu needs to be its own component? I am attempting to put the code in app.component.html and then hit the problem.Sabo
try clearing 'npm cache clean --force' then delete 'package.lock' and do 'npm install'Erudition
Try moving dropdown code to another component and tryErudition
Thanks for the help, the issue is described in my answer.Sabo
P
71

Make sure your HTML markup has the ngbDropdown attribute on ngbDropdownToggle and ngbDropdownMenu parent.

<div ngbDropdown> <!-- **here** -->
  <button class="btn btn-outline-primary" 
      id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown</button>
  <div ngbDropdownMenu>
    <button ngbDropdownItem>Action - 1</button>
    <button ngbDropdownItem>Another Action</button>
    <button ngbDropdownItem>Something else is here</button>
  </div>
</div>
Pennant answered 5/8, 2019 at 17:34 Comment(1)
YES, we cannot have ngbDropdownMenu or ngbDropdownToggle component without a parent ngbDropdown component. github.com/ng-bootstrap/ng-bootstrap/issues/…Mani
S
2

As provided by @robert, with the official example, the issue is that the pulldown menu html must not be directly in the app.component.html. If placed there, the stated error appears.

I am not sure if placing the pulldown menu html in its own component is the only solution, but it is the difference between working and not working for my situation. I was unable to place the content found in the dropdown-basic.html into the app.component.html without encountering the issue.

I would suggest to the authors of angular bootstrap that the required usage be placed directly in the documentation. As it is now, it is not explicit. The official example simply appears as one way to use the code.

Sabo answered 21/2, 2019 at 21:23 Comment(0)
N
0

I was getting this error with a standalone component, I needed to add NgbDropdownModule to my component imports array, per the example and now it works.

Nonfulfillment answered 27/12, 2023 at 17:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.