Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap Dropdowns

Ngx Bootstrap Dropdowns

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.

BsDropdownDirective

selector

  • [bsDropdown],[dropdown]

Inputs

  • AutoClose- boolean, means that the drop-down on the item or document click is closed, and after pressing ESC
  • Container − string, A selector indicating the element to which the popover should be appended.
  • dropup − boolean, This attribute means that the drop-down can open upwards.
  • InsideClick − Boolean, this attribute means that if autoClose is set to true, the dropdown does not close on the inside click.
  • isAnimated −Boolean is animated, meaning that the dropdown will be animated
  • isDisabled − boolean, Disables drop-down toggle and hides drop-down menu when opened is Disabled
  • IsOpen - Boolean, returns whether the popover is currently displayed or not.
  • placement − String positioning, Placement of a popover. Accepts: "top", "bottom", "left", "right"
  • Triggers − String, Defines events to trigger. A space-separated list of event names is supported.

Outputs

  • isOpenChange-Emit an occurrence when change isOpen
  • onHidden − When the popover is hidden, onHidden-emits an event
  • onShown- Issues an event when the popover is shown

Methods

  • show() − Opens the popover feature. This is considered a 'manual' popover trigger.
  • hide()-closes popover of an element. This is considered a 'manual' popover trigger.
  • toggle()-Toggles the popover for an element. This is considered a 'manual' popover trigger.
  • setConfig() − Set popover config

Example

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 {}
}

Build and Serve

Run the following command to start the angular server.

ng serve