如何使用 Angular4 按元素 ID 设置焦点



我是Angular的新手,正在尝试使用它来设置ID为"input1"的输入的焦点。 我正在使用以下代码:

@ViewChild('input1') inputEl: ElementRef;

然后稍后在组件中:

 this.inputEl.nativeElement.focus();

但它不起作用。 我做错了什么? 任何帮助将不胜感激。

组件

import { Component, ElementRef, ViewChild, AfterViewInit} from '@angular/core';
... 
@ViewChild('input1', {static: false}) inputEl: ElementRef;
    
ngAfterViewInit() {
   setTimeout(() => this.inputEl.nativeElement.focus());
}

.HTML

<input type="text" #input1>

@Z.Bagley提到的问题中的一个答案给了我答案。 我必须将 Renderer2 从 @angular/core 导入到我的组件中。然后:

const element = this.renderer.selectRootElement('#input1');
setTimeout(() => element.focus(), 0);

谢谢@MrBlaise的解决方案!

经过一些搜索,我也遇到了同样的问题,我找到了一个很好的解决方案,因为@GreyBeardedGeek提到 setTimeout 是这个解决方案的关键。他是完全正确的。在您的方法中,您只需要添加 setTimeout,您的问题就会得到解决。

setTimeout(() => this.inputEl.nativeElement.focus(), 0);

这是一个 Angular4+ 指令,您可以在任何组件中重用。基于Niel T在这个问题中的回答中给出的代码。

import { NgZone, Renderer, Directive, Input } from '@angular/core';
@Directive({
    selector: '[focusDirective]'
})
export class FocusDirective {
    @Input() cssSelector: string
    constructor(
        private ngZone: NgZone,
        private renderer: Renderer
    ) { }
    ngOnInit() {
        console.log(this.cssSelector);
        this.ngZone.runOutsideAngular(() => {
            setTimeout(() => {
                this.renderer.selectRootElement(this.cssSelector).focus();
            }, 0);
        });
    }
}

您可以在组件模板中使用它,如下所示:

<input id="new-email" focusDirective cssSelector="#new-email"
  formControlName="email" placeholder="Email" type="email" email>

为输入提供一个 id,并将该 id 传递给指令的 cssSelector 属性。或者你可以传递任何你喜欢的cssSelector。

尼尔·

由于我唯一要做的就是将焦点放在一个元素上,所以我 不需要关心更改检测,所以我实际上可以 在 Angular 之外运行对 renderer.selectRootElement 的调用。因为 我需要给新部分时间来渲染,元素部分是 包装在超时中,以允许渲染线程有时间赶上 在尝试选择元素之前。一旦设置好了,我 可以简单地使用基本的 CSS 选择器调用元素。

这对

我有帮助(在离子中,但想法是一样的)https://mhartington.io/post/setting-input-focus/

在模板中:

<ion-item>
      <ion-label>Home</ion-label>
      <ion-input #input type="text"></ion-input>
</ion-item>
<button (click)="focusInput(input)">Focus</button>

在控制器中:

  focusInput(input) {
    input.setFocus();
  }

下面是可以在任何组件中使用的指令:

import { NgZone, Directive, ElementRef, AfterContentInit, Renderer2 } from '@angular/core';
@Directive({
    selector: '[appFocus]'
})
export class FocusDirective implements AfterContentInit {
    constructor(private el: ElementRef, private zone: NgZone, private renderer: Renderer2) {}
    ngAfterContentInit() {
        this.zone.runOutsideAngular(() => setTimeout(() => {
            this.renderer.selectRootElement(this.el.nativeElement).focus();
        }, 0));
    }
}

用:

<input type="text" appFocus>

添加组件

ngOnInit(): void {
    document?.getElementById("elementToGiveFocus")?.focus();
}
您可以

简单地使用以下示例所示的scrollIntoView,并随时调用该方法。

<div id="elementID"></div>
ngAfterViewInit(): void 
{
    const element = document.querySelector("#elementID");
    element.scrollIntoView(true);
}

最新更新