1

按下键盘上的任意键时如何在 mat-input 中聚焦。

试过:

@HostListener('document:keydown', ['this.search.nativeElement.focus()'])
4

2 回答 2

2

尝试这个:

@HostListener('document:keypress', ['$event'])
    handleKeyboardEvent(event: KeyboardEvent) {
        this.search.nativeElement.focus();
    }
于 2020-07-22T09:03:52.937 回答
0

YAY 修复了,谢谢

 @HostListener('document:keypress', ['$event'])
  handleKeyboardEvent(event: KeyboardEvent) {
    this.search.nativeElement.focus();
    this.searchFocus();
  }

  keyboardFocusOut() : void {
    this.searchText = ""
    this.search.nativeElement.blur();
    this.searchFocusOut();
  }
于 2020-07-22T13:57:29.040 回答