问题
i'm using ng-bootstrap and i want to get the value of dropdown when selected .
<div class="col text-right">
<div ngbDropdown placement="top-right" class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic2" ngbDropdownToggle>Toggle dropup</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic2">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
回答1:
You need to add a click event:
<button class="dropdown-item" (click)="changeAction(obj)">
In the component:
changeAction(obj) {
//your logic here
}
回答2:
This runs without any javascript backend code. Solution is build upon angular template variables. Until no item is selected a place holder text "Select Environment" is displayed.
<div ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdown"
ngbDropdownToggle>{{ selectedValue.value || "Select Environment" }}</button>
<div ngbDropdownMenu aria-labelledby="dropdown" #selectedValue>
<div *ngFor="let env of environments;" >
<button ngbDropdownItem id="option" on-click="selectedValue.value=env">{{env}}
</button>
</div>
</div>
</div>
来源:https://stackoverflow.com/questions/46318316/how-to-take-the-value-of-dropdown-in-ng-bootstrap