简体   繁体   English

在焦点上打开 ngx-bootstrap typeahead

[英]opening ngx-bootstrap typeahead on focus

Is there any way to open the ngx-typeahead when a user enters the input.当用户输入输入时,有没有办法打开 ngx-typeahead。 In other words, on focus换句话说,在focus

在此处输入图片说明

I can get the dropdown to open if I type a single character into the input box, and then hit backspace.如果我在输入框中输入一个字符,然后按退格键,我可以打开下拉菜单。

在此处输入图片说明

只需将此添加到输入

[typeaheadMinLength]="0"

Setting [typeaheadMinLength]="0" is the way to do this, but it also has the side effect of triggering a search even when nothing is typed in yet.设置[typeaheadMinLength]="0"是这样做的方法,但它也有触发搜索的副作用,即使尚未输入任何内容。 I my case I needed to run the search on focus, but only if the textbox already had some content.我的情况是我需要在焦点上运行搜索,但前提是文本框已经有一些内容。 I did this using a reference to the input's value:我使用对输入值的引用来做到这一点:

<input #searchBox
[typeaheadMinLength]="searchBox.value?.length > 0 ? 0 : 1"
/>

You will need to do two things to make this possible:您需要做两件事才能做到这一点:

First, set [typeaheadMinLength]="0" to have the input open when nothing has been typed yet.首先,设置 [typeaheadMinLength]="0" 以在尚未输入任何内容时打开输入。

Secondly, since the type ahead input is the first field in the form, you may need to add a small delay by also setting [typeaheadWaitMs]="300 (or some value). This is because the input may receive focus before the values are available.其次,由于提前输入是表单中的第一个字段,您可能需要通过设置 [typeaheadWaitMs]="300(或某个值)来添加一个小的延迟。这是因为输入可能会在值之前获得焦点可用。

Example:示例:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM