1) 구성 요소의 템플릿에 입력 템플릿 변수 참조를 추가
<ion-input #textInput>
2) 구성 요소의 수입에 ElementRef
및 ViewChild
을 추가
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()? – kit
이온 입력이라고 깜빡 했네. 따라서 blur()와 focus() int는 네이티브 요소가 아닙니다. – Natanael