Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

486
Visualizações
How to remove focus from a ion-input in Angular 2+/Ionic2+

It is possible to set focus using setFocus method from nativeEloment.

But how about remove focus?

How could I remove the cursor from and unselect an input from a template in a Angular 2+/Ionic 3 app?

I need to remove the focus because the mobile keyboard keeps open while a input has focus.

Edit: The input is a ion-input of Ionic2+.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

1) Add a template variable reference to your input in your component's template:

<ion-input #textInput>

2) Add ElementRef and ViewChild to your component's imports:

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

3) Add @ViewChild variable and relevant methods to your component:

export class AppComponent {

  @ViewChild('textInput') textInput;

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

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

}

You can trigger setFocus() or removeFocus() in a number of ways. Here's an example:

# 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();
    }
  }
}
about 4 years ago · Santiago Trujillo Relatório

0

For Ionic 4 users ;)

After trying multiple solution to dismiss/hide the keyboard of a search input, I found a way inspired from your solution @zach-newburgh.

Was not working :

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

Only working solution

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

Complete solution

TS way

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 and fastest way

html

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

Hope it will help.

about 4 years ago · Santiago Trujillo Relatório

0

Apply focus and remove Focus Based on condition:

if (condition) {
  this.addressLine1.nativeElement.focus();
}else{
  this.addressLine1.nativeElement.blur();
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda