按下回车键时如何取消聚焦离子输入

How to unfocus ion-input when enter is hit

我有一个 ion-input 允许用户添加文本评论。 我希望当文本不为空并且用户点击 enter 时,离子输入自动散开。当然,我会实现将评论文本保存到DB并在页面中显示的逻辑。

我尝试了 event.blur()textInput.nativeElement.blur(),但没有用。 如何散焦?

  commentHandler(event) {
    const keyCode = event.keyCode;
    if (keyCode === 13 && this.commentText != "") {
      this.logger.info("enter hit");
      event.blur();
      // this.textInput.nativeElement.blur();
    }
  }

<ion-item>
    <ion-label floating>Add comment, then hit enter</ion-label>
    <ion-input #commentInput type="text" (keypress)="commentHandler($event)" ngModel="commentText"></ion-input>
  </ion-item>

在搜索和尝试不同的东西之后,这似乎对我有用。希望对你有帮助。

myPage.ts:

import { ElementRef, ViewChild } from "@angular/core";

export class MyPage {
    @ViewChild('chatInput') chatInput: ElementRef;

    myFunction() {
        this.chatInput['_native'].nativeElement.blur();
    }
}

myPage.html:

<ion-input #chatInput ></ion-input>

受此页面启发(但他们的解决方案对我不起作用): https://forum.ionicframework.com/t/how-to-force-blur-on-ion-input/64038/6