how to take the value of Dropdown in ng-bootstrap?

走远了吗. 提交于 2020-08-19 16:49:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!