如何在 Angular 2 NgForm 上观察触摸事件? [英] How to observe touched event on Angular 2 NgForm?

查看:30
本文介绍了如何在 Angular 2 NgForm 上观察触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以订阅 NgFormvalueChanges 可观察属性的回调,以便对表单控件值的变化做出反应.

我需要以同样的方式对用户触摸表单控件之一的事件做出反应.

这个类 似乎定义了 valueChanges Observable 并且 touched 属性被定义为一个布尔值.

有没有办法对控制触摸"事件做出反应?

解决方案

ng2 没有提供直接的方式来对触摸事件做出反应.它使用 (input) 事件来触发 valueChanges 事件和 (blur) 事件来设置 touched/untouched 属性AbstractControl.因此,您需要在模板中手动订阅所需事件并在您的组件类中处理它.

It is possible to subscribe a callback to an NgForm's valueChanges observable property in order to react to changes in the values of the controls of the form.

I need, in the same fashion, to react to the event of the user touching one of the form controls.

This class seem to define the valueChanges Observable and the touched property is defined as a boolean.

Is there a way to to react to the "control touched" event?

解决方案

There is not direct way provided by ng2 to react on touched event. It uses (input) event to fire the valueChanges event and (blur) event to set touched/untouched property of AbstractControl. So you need to manually subscribe on desired event in the template and handle it in your component class.

这篇关于如何在 Angular 2 NgForm 上观察触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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