如何在Angular或Angular 2而不是AngularJS中设置自动对焦 [英] How to set autofocus in angular or angular 2 not angularjs

查看:76
本文介绍了如何在Angular或Angular 2而不是AngularJS中设置自动对焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了很多关于如何通过创建指令或HTML自动对焦来对angularjs的输入设置自动对焦的帖子,有没有一种快速简便的方法来设置角度(角度2,角度4等)中的焦点

I see many posts in how to set autofocus on an input filed in angularjs by creating a directive or HTML autofocus, Is there a quick and easy way to set focus in angular (angular 2, angular 4 etc)

推荐答案

在您的html中添加#nameit以获取其在组件代码中的引用.

In your html add #nameit to get its reference in component code.

<input type="text" name="me" #nameit>

然后在其中应用自动注浆.

Then apply auto fouces into it.

  @ViewChild('nameit') private elementRef: ElementRef;

  public ngAfterViewInit(): void {
    this.elementRef.nativeElement.focus();
  }

这篇关于如何在Angular或Angular 2而不是AngularJS中设置自动对焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆