有没有一种Angular2方式可以专注于输入字段? [英] Is there an Angular2 way to focus on an input field?

查看:55
本文介绍了有没有一种Angular2方式可以专注于输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法结束时,我将清除一些字段,这很容易:

At the end of a method, I'll be clearing some fields, which is easy enough:

this.modelname.fieldname1 = "";
this.modelname.fieldname2 = "";

清除这些字段后,我希望光标出现在field1中.

After clearing the fields, I'd like the cursor to appear in field1.

是否有Angular2方式可以做到这一点,还是只使用老式的Javascript?

Is there an Angular2 way to do this, or do I just use old fashioned Javascript?

推荐答案

您可以在第一个输入中使用模板引用变量,这将使您能够在其上运行.focus().

You can use a template reference variable in the first input, that will give you the ability to run .focus() on it.

在模板中,您可以将#fieldName1添加到输入标签(即<input type="text" #fieldName1>)

In your template you can add #fieldName1 to your input tag (i.e. <input type="text" #fieldName1>)

在您的控制器中执行以下操作:

in your controller do:

@ViewChild('fieldName1')
fieldName1: any;

现在您可以在控制器中执行this.fieldName1.nativeElement.focus()或在模板中执行fieldName1.focus().

now you can do this.fieldName1.nativeElement.focus() in the controller or fieldName1.focus() in the template.

这篇关于有没有一种Angular2方式可以专注于输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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