【问题标题】:How to remove focus from a ion-input in Angular 2+/Ionic2+如何从 Angular 2+/Ionic2+ 中的离子输入中移除焦点
【发布时间】:2017-09-30 11:48:08
【问题描述】:

可以使用 nativeEloment 的 setFocus 方法设置焦点。

但是移除焦点怎么样?

如何在 Angular 2+/Ionic 3 应用程序中从模板中删除光标并取消选择输入?

我需要移除焦点,因为移动键盘在输入具有焦点时保持打开状态。

编辑:输入是 Ionic2+ 的离子输入。

【问题讨论】:

  • blur() 怎么样?
  • 我忘了说这是离子输入。所以它的原生元素中没有 blur() 和 focus()。

标签: angular mobile input ionic2 focus


【解决方案1】:

1) 在组件的模板中添加对输入的模板变量引用:

<ion-input #textInput>

2) 将 ElementRefViewChild 添加到组件的导入中:

import { ElementRef, ViewChild } from '@angular/core'

3) 将@ViewChild 变量和相关方法添加到您的组件中:

export class AppComponent {

  @ViewChild('textInput') textInput;

  setFocus() {
    this.textInput.nativeElement.focus();
  }

  removeFocus() {
    this.textInput.nativeElement.blur();
  }

}

您可以通过多种方式触发setFocus()removeFocus()。这是一个例子:

# app.component.html

<ion-input #textInput>

# app.component.ts

import { HostListener } from '@angular/core';

export class AppComponent {

  [previous code elided for readability]

  isInputActive: boolean;

  @HostListener('document:click', ['$event'])
  handleClickEvent(event: MouseEvent): void {
    if (document.activeElement !== this.textInput.nativeElement) {
      this.textInput.nativeElement.blur();
    }
  }
}

【讨论】:

  • 我忘了说这是离子输入。所以它的原生元素中没有 blur() 和 focus()。
  • 根据documentation,它应该可以工作:this.textInput 的返回值是多少?但这个问题与(mouseover)(mouseout) 有关。我将从示例中删除它!
【解决方案2】:

对于 Ionic 4 用户 ;)

在尝试了多种解决方案来关闭/隐藏搜索输入的键盘后,我找到了一种从您的解决方案 @zach-newburgh 中获得灵感的方法。

没有工作:

  • this.keyboard.hide()
  • #searchInput (keyup.enter)="searchInput.blur()"
  • this.searchInput.nativeElement.blur();
  • this.searchInput.getElementRef().nativeElement.blur();

唯一有效的解决方案

this.searchInput._native.nativeElement.blur();

完整的解决方案

TS方式

ts

import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { TextInput } from 'ionic-angular';

...

@ViewChild('searchInput') searchInput: TextInput;

...

onSearchEnter() {
    this.searchInput._native.nativeElement.blur();
}

html

<ion-input #searchInput
           type="search"
           (keyup.enter)="onSearchEnter()"
           [(ngModel)]="searchQuery"></ion-input>

HTML 和最快的方式

html

<ion-input #searchInput
           type="search"
           (keyup.enter)="searchInput._native.nativeElement.blur()"
           [(ngModel)]="searchQuery"></ion-input>

希望它会有所帮助。

【讨论】:

    【解决方案3】:

    根据条件应用焦点和移除焦点:

    if (condition) {
      this.addressLine1.nativeElement.focus();
    }else{
      this.addressLine1.nativeElement.blur();
    }
    

    【讨论】:

    • 正如问题中提到的it's a ion-input. So it's do not have blur() and focus() int its native element.这个答案无济于事。
    【解决方案4】:

    这在 ionic 5 上对我有用

    this.searchInput.getInputElement().then((input) => {
      input.blur();
    });
    

    【讨论】:

    • 感谢 James D。您的方法有效并帮助我解决了 Ionic 5 中的焦点问题。
    【解决方案5】:
    Declare veiwChild with one public variable
    @ViewChild('myActionForm', { static: false, read: ElementRef }) public actionForm: ElementRef<any>;
    **add set Timeout with in the method**
            setTimeout(() => {
              this.actionForm.nativeElement.click();
            }, 100);
    

    【讨论】:

    猜你喜欢
    • 2019-03-24
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 2019-11-30
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多