To access the dropdown from your TS file, you can utilize @ViewChild()
.
Include a template variable like #dropdown
in the ngbDropdown
element.
<div #dropdown ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown
</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button ngbDropdownItem>Action - 1</button>
<button ngbDropdownItem>Another Action</button>
<button ngbDropdownItem>Something else is here</button>
</div>
</div>
Then, in your typescript file, utilize @ViewChild()
. This will allow you to call the open method on the dropdown from within the typescript.
@Component({
selector: 'ngbd-dropdown-basic',
templateUrl: './dropdown-basic.html',
})
export class NgbdDropdownBasic {
@ViewChild(NgbDropdown, { static: true })
public dropdown: NgbDropdown;
public toggleDropdown(): void {
this.dropdown.open();
}
}
For a functional example, check out this Stackblitz link...
https://stackblitz.com/edit/angular-7vbmva