gpt4 book ai didi

javascript - ng bootstrap 4 - typeahead 打开焦点

转载 作者:行者123 更新时间:2023-11-30 07:32:15 24 4
gpt4 key购买 nike

我使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com