NgbTypeahead selectItem get clicked item ngBootstrap angular2

痴心易碎 提交于 2019-11-27 07:36:04

问题


In this answer, it was explained to me to use selectItem to get the select event.

But at this point, the model I bound to the text box is still the original text the user typed, not the select item.

I use

(selectItem)="search(model)"

to get the event, and in TS

search(model) { 
this._service.search(model).subscribe(
  results => this.results = results,
  error => this.errorMessage = <any>error);

}

but as mentioned above, that calls my backend with the user-typed text, not the full text of the selected item of the typeahead.

My backend logs

2017/03/24 20:44:14 /api/typeahead/ok
2017/03/24 20:44:14 /api/search/ok

where the second should be /api/search/$actualSelectedItem.


回答1:


You should be using $event to get the selected Items as below

<input type="text" class="form-control" (selectItem)="selectedItem($event)" [(ngModel)]="model" [ngbTypeahead]="search" [resultFormatter]="formatter" />
<hr>
<pre>Model: {{ model | json }}</pre>
clicked item {{clickedItem}}

Your method should be as

selectedItem(item){
    this.clickedItem=item.item;
    console.log(item);
  }

LIVE DEMO



来源:https://stackoverflow.com/questions/43007815/ngbtypeahead-selectitem-get-clicked-item-ngbootstrap-angular2

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