Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No provider for NgbDropdown with Angular Bootstrap

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://stackoverflow.com/a/46636887/2178363

like image 776
Jason K. Avatar asked Feb 21 '19 20:02

Jason K.


2 Answers

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>
like image 178
O.O Avatar answered Oct 23 '22 17:10

O.O


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.

like image 2
Jason K. Avatar answered Oct 23 '22 19:10

Jason K.