NgbTypeahead selectItem如何获取被点击的项(ngBootstrap angular2)

22
在这个回答中,有人向我解释了使用selectItem来获取选择事件。但是此时,我绑定到文本框的模型仍然是用户输入的原始文本,而不是选择项。
我使用了。
(selectItem)="search(model)"

获取事件,在TS中

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

但是如上所述,这将使用用户键入的文本调用我的后端,而不是typeahead选定项目的完整文本。

我的后端记录

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

第二个应该是 /api/search/$actualSelectedItem。

1个回答

68
你应该使用以下代码中的$event来获取选定的项目。
<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);
  }

在线演示


现在看起来好像很容易。谢谢。 - Wilbert
1
如果有人需要,这里有一些文档:https://ng-bootstrap.github.io/#/components/typeahead/api#NgbTypeahead - Fariz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接