NgbTypeahead selectItem get clicked item ngBootstrap angular2

In this answer, they explained to me that selectItem uses select select.

But at this point, the model tied to the text field is still the source text entered by the user, and not the selection item.

I use

(selectItem)="search(model)"

to receive the event as in TS

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

}

but, as mentioned above, this calls my backend with user-entered text, not the full text of the selected typeahead element.

My backend magazines

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.

+4
source share
1 answer

You must use $eventto get the selected items 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}}

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

LIVE DEMO

+16

Source: https://habr.com/ru/post/1673160/


All Articles