Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap Dropdowns
The dropdown component of ngx-bootstrap is toggleable and provides contextual overlay to view link list, etc. We can make dropdowns interactive with dropdown directives.
selector
Inputs
Outputs
Methods
As we're going to use dropdowns, we have to update app.module.ts to use BsDropdownModule and BsDropdownConfig in the ngx-bootstrap DatePicker chapter.
To use BsDropdownModule and BsDropdownConfig, update app.module.ts.
app.module.ts
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { AppComponent } from './app.component'; import { TestComponent } from './test/test.component'; import { AccordionModule } from 'ngx-bootstrap/accordion'; import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert'; import { ButtonsModule } from 'ngx-bootstrap/buttons'; import { FormsModule } from '@angular/forms'; import { CarouselModule } from 'ngx-bootstrap/carousel'; import { CollapseModule } from 'ngx-bootstrap/collapse'; import { BsDatepickerModule, BsDatepickerConfig } from 'ngx-bootstrap/datepicker'; import { BsDropdownModule,BsDropdownConfig } from 'ngx-bootstrap/dropdown'; @NgModule({ declarations: [ AppComponent, TestComponent ], imports: [ BrowserAnimationsModule, BrowserModule, AccordionModule, AlertModule, ButtonsModule, FormsModule, CarouselModule, CollapseModule, BsDatepickerModule.forRoot(), BsDropdownModule ], providers: [AlertConfig, BsDatepickerConfig, BsDropdownConfig], bootstrap: [AppComponent] }) export class AppModule { }
Update test.component.html to use the dropdowns.
test.component.html
<div class="btn-group" dropdown #dropdown="bs-dropdown" [autoClose]="false"> <button id="button-basic" dropdownToggle type="button" class="btn btn-primary dropdown-toggle" aria-controls="dropdown-basic"> Menu <span class="caret"></span> </button> <ul id="dropdown-basic" *dropdownMenu class="dropdown-menu" role="menu" aria-labelledby="button-basic"> <li role="menuitem"><a class="dropdown-item" href="#">File</a></li> <li role="menuitem"><a class="dropdown-item" href="#">Edit</a></li> <li role="menuitem"><a class="dropdown-item" href="#">Search</a></li> <li class="divider dropdown-divider"></li> <li role="menuitem"><a class="dropdown-item" href="#">Recents</a> </li> </ul> </div> <button type="button" class="btn btn-primary" (click)="dropdown.isOpen = !dropdown.isOpen">Show/Hide </button>
Update test.component.ts for corresponding variables and methods.
test.component.ts
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] }) export class TestComponent implements OnInit { constructor() {} ngOnInit(): void {} }
Run the following command to start the angular server.
ng serve