使用屏幕键盘时,自定义 HTML 元素输入字段中的表单值不显示

发布于 2025-01-17 06:04:36 字数 1343 浏览 0 评论 0原文

我正在尝试获取使用屏幕键盘输入到自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2025-01-24 06:04:36

问题:

您正在将指令添加到自定义元素&监听自定义元素而不是实际输入字段上的事件。因为,您有 @HostListener 用于 focus &指令内的模糊事件,在自定义组件的情况下这些事件不会触发。因为您的自定义组件不会触发 focusblur 事件。 (如果我们希望任何自定义元素可聚焦,我们添加 tabindex=0 以使其按照网页的自然 Tab 键顺序可聚焦)。

为什么它适用于本机输入元素?

因为本机输入元素发出 focus & 模糊事件。因此,当您将该指令应用于本机输入时,您可以看到虚拟键盘。

解决方案:

您需要将事件侦听器添加到自定义组件内的本机输入字段。由于该指令是通用的,因此您可以在 OskInputDirective 内的 nativeElement 上使用 querySelector() 来访问特定的输入字段。

OskInputDirective中,添加focus & ngAfterViewInit 中的 blur 监听器:

 ngAfterViewInit(){
    this.inputEl=this.el.nativeElement.querySelector('input');
    this.inputEl?.addEventListener('focus',this.onFocus.bind(this));
    this.inputEl?.addEventListener('blur',this.onBlur.bind(this));
  }

现在,您可以使用 this.inputEl 来引用 onKey() 中的输入元素代码>方法。

注1:在这里,我们将回调与OskInputDirective的上下文绑定。否则,this 将引用 DOM 事件。

注2:由于我们手动订阅 DOM 事件,Angular 将无法检测到更改和更改。更新您的 FormControl 值。当您更改虚拟键盘的输入值时,您需要发出一个事件。

onKey()中,最后调用事件发射器。现在,在父级和父级中收听此事件更新相应的表单控件值:

this.inputChanged.emit(element.value);
    this.updateScrollPosition();

这是分叉的 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 for focus & blur events inside the directive, these events don't fire in case of custom component. Because your custom component doesn't fire focus and blur events. (If we want any custom element to be focusable, we add tabindex=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 native input.

Solution:

You need to add event listeners to the native input field inside your custom component. Since, the directive is generic, you can use querySelector() on the nativeElement inside OskInputDirective to access the particular input field.

In OskInputDirective, add focus & blur listeners in ngAfterViewInit:

 ngAfterViewInit(){
    this.inputEl=this.el.nativeElement.querySelector('input');
    this.inputEl?.addEventListener('focus',this.onFocus.bind(this));
    this.inputEl?.addEventListener('blur',this.onBlur.bind(this));
  }

Now, you can use this.inputEl to refer to the input element inside your onKey() 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 the input 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:

this.inputChanged.emit(element.value);
    this.updateScrollPosition();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文