使用屏幕键盘时,自定义 HTML 元素输入字段中的表单值不显示
我正在尝试获取使用屏幕键盘输入到自定义 HTML 元素输入字段中的值以进行显示。
但是我目前得到的是空字符串。如果我使用普通的输入字段,那么它就可以正常工作。
这是我的代码片段和一个 stackblitz 示例,其中包含详细代码。
HTML
<form-input
appOskInput
[formStatus]="formStatus"
[parentFormGroup]="setupForm"
[data]="{
formControlName: 'email',
name: 'email'
}">
</form-input>
<app-keyboard></app-keyboard>
键盘指令
private onKey(key: string) {
let element = this.el.nativeElement;
let inputEl = element.querySelector('input');
let start = inputEl.selectionStart;
let end = inputEl.selectionEnd;
this.measure.textContent = inputEl.value.substr(0, start) + key;
inputEl.value = inputEl.value.substr(0, start) + key + inputEl.value.substr(end);
if (this.control)
this.control.control.setValue(inputEl.value)
inputEl.focus();
inputEl.selectionStart = inputEl.selectionEnd = start + 1;
this.updateScrollPosition();
}
这是 stackblitz https:/ /stackblitz.com/edit/onscreen-keyboard-hlmkvv?file=src/app/app.component.ts
您将看到从普通输入字段输入时会显示这些值,但我使用我的自定义 HTML 元素输入字段时无法发生相同的情况。我已经在 HTML 页面的代码中注释掉了自定义 HTML 输入字段。有人可以帮忙吗?
I am trying to get values that I type using an on-screen keyboard into a custom HTML element input field to display.
However I am currently getting empty strings. If I use a normal input field then it works fine.
Here is a snippet of my code and a stackblitz example which will have detailed code.
HTML
<form-input
appOskInput
[formStatus]="formStatus"
[parentFormGroup]="setupForm"
[data]="{
formControlName: 'email',
name: 'email'
}">
</form-input>
<app-keyboard></app-keyboard>
Keyboard directive
private onKey(key: string) {
let element = this.el.nativeElement;
let inputEl = element.querySelector('input');
let start = inputEl.selectionStart;
let end = inputEl.selectionEnd;
this.measure.textContent = inputEl.value.substr(0, start) + key;
inputEl.value = inputEl.value.substr(0, start) + key + inputEl.value.substr(end);
if (this.control)
this.control.control.setValue(inputEl.value)
inputEl.focus();
inputEl.selectionStart = inputEl.selectionEnd = start + 1;
this.updateScrollPosition();
}
Here is the stackblitz https://stackblitz.com/edit/onscreen-keyboard-hlmkvv?file=src/app/app.component.ts
You will see that the values are displaying when inputted from a normal input field but I can't get the same to happen when using my custom HTML element input field. I have commented out the custom HTML input field in the code on the HTML page. Can anyone help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题:
您正在将指令添加到自定义元素&监听自定义元素而不是实际输入字段上的事件。因为,您有
@HostListener
用于focus
&指令内的模糊
事件,在自定义组件的情况下这些事件不会触发。因为您的自定义组件不会触发focus
和blur
事件。 (如果我们希望任何自定义元素可聚焦,我们添加tabindex=0
以使其按照网页的自然 Tab 键顺序可聚焦)。为什么它适用于本机输入元素?
因为本机输入元素发出
focus
&模糊
事件。因此,当您将该指令应用于本机输入
时,您可以看到虚拟键盘。解决方案:
您需要将事件侦听器添加到自定义组件内的本机
输入
字段。由于该指令是通用的,因此您可以在OskInputDirective
内的 nativeElement 上使用querySelector()
来访问特定的输入字段。在
OskInputDirective
中,添加focus
&ngAfterViewInit
中的blur
监听器:现在,您可以使用
this.inputEl
来引用onKey()
中的输入元素代码>方法。注1:在这里,我们将回调与
OskInputDirective
的上下文绑定。否则,this
将引用 DOM 事件。注2:由于我们手动订阅 DOM 事件,Angular 将无法检测到更改和更改。更新您的
FormControl
值。当您更改虚拟键盘的输入值时,您需要发出一个事件。在
onKey()
中,最后调用事件发射器。现在,在父级和父级中收听此事件更新相应的表单控件值:这是分叉的 stackblitz,其中创建了一个自定义输入元素以供参考。 https://stackblitz.com/edit/ onscreen-keyboard-na5kqr?file=src/app/app.component.ts
Issue:
You are adding the directive to a custom element & listening the events on the custom element instead of actual input field. Since, you have
@HostListener
forfocus
&blur
events inside the directive, these events don't fire in case of custom component. Because your custom component doesn't firefocus
andblur
events. (If we want any custom element to be focusable, we addtabindex=0
to make it focusable in the natural tabbing order of the webpage).Why it work on native input elements?
Because native input elements emit
focus
&blur
events. Therefore, you are able to see the virtual keyboard when you apply the directive to nativeinput
.Solution:
You need to add event listeners to the native
input
field inside your custom component. Since, the directive is generic, you can usequerySelector()
on the nativeElement insideOskInputDirective
to access the particular input field.In
OskInputDirective
, addfocus
&blur
listeners inngAfterViewInit
:Now, you can use
this.inputEl
to refer to the input element inside youronKey()
method.Note 1: Here, we are binding the callbacks with the context of
OskInputDirective
. Else,this
will refer to the DOM event.Note 2: Since we are manually subscribing to the DOM event, Angular won't be able to detect changes & update your
FormControl
value. You need to emit an event when you change theinput
value from virtual keyboard.In
onKey()
, call event emitter at the end. Now, listen to this event in the parent & update the corresponding form control value:Here is the forked stackblitz with a custom input element created for reference. https://stackblitz.com/edit/onscreen-keyboard-na5kqr?file=src/app/app.component.ts