javascript - ng bootstrap 4 - typeahead 打开焦点

标签 javascript angular typescript bootstrap-4 ng-bootstrap

我使用 ng-bootstrap 4(测试版 8)。目前我有这个:

<ng-template #rt let-r="result" let-t="term">
    {{ r.label }}
</ng-template>

<input
        id="typeahead-focus"
        class="form-control"
        [(ngModel)]="model"
        [ngbTypeahead]="search"
        [inputFormatter]="formatter"
        [resultTemplate]="rt"
        (focus)="focus$.next($event.target.value)"
        (click)="click$.next($event.target.value)"
        #instance="ngbTypeahead"
/>

现在,如果用户单击输入元素,我想打开 typeahead。我该怎么做?

this.search = (text$) =>
    text$
        .map(term => (term === '' ? this.items : this.items.filter(v => v.label.toLowerCase().indexOf(term.toLowerCase()) > -1)).slice(0, 10));

this.formatter = (x: {label: string}) => {
    console.log(x);
    return x.label;

最佳答案

以下解决方案适用于我:

onFocus 事件添加到您的输入搜索中

我的.html文件

 <input 
    (focus)="onFocus($event)" 
    type="text" 
    (selectItem)="onItemSelected($event)" 
    [(ngModel)]="myModel" 
    [ngbTypeahead]="search" 
    [resultTemplate]="rt" 
    [inputFormatter]="formatter"/>

我的.ts文件

  public onFocus(e: Event): void {
    e.stopPropagation();
    setTimeout(() => {
      const inputEvent: Event = new Event('input');
      e.target.dispatchEvent(inputEvent);
    }, 0);
  }

  search = (text$: Observable<string>) =>
    text$
      .debounceTime(200)
      .distinctUntilChanged()
      .map(term => this.myList
          .filter(v => this.myfilter(term))
          .slice(0, 10));

另请查看 Typeahed: allow search on focus #698

关于javascript - ng bootstrap 4 - typeahead 打开焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47946603/

相关文章:

javascript - Angular 2/4 动态加载脚本

javascript - 限制 Typescript 对象实例化的属性

javascript - 如何从嵌套模块导入?

javascript - socket.io游戏开发算法

Angular 10 : Import using module alias not resolved/Path Mapping not recognised in VSCode

reactjs - 从传递给通用 React 组件的回调返回的对象属性的 Typescript 验证?

node.js - 路由无法正常工作

javascript - Ajax 请求加载时间过长

javascript - JointJS - 在链接上使用路由器表现得很奇怪

javascript - iWebInspector 中的 Console.log 输出