Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在角度反应型场上捕捉接触事件?_Angular_Typescript - Fatal编程技术网

Angular 如何在角度反应型场上捕捉接触事件?

Angular 如何在角度反应型场上捕捉接触事件?,angular,typescript,Angular,Typescript,我一直在寻找解决方案,但找不到我想要的。我有这个表格,一旦任何字段被触及,我想在ngOnInit中发出一条消息。到目前为止,如果我在任何字段中输入一个值,我可以显示一条消息(“您输入了一个值”),但我希望能够在用户触摸该字段时显示一条消息。谁能给我指出正确的方向吗?这是我的密码 提前多谢 ngOnInit() { this.myForm.valueChanges.subscribe(res => { console.log("You entered a value"); //

我一直在寻找解决方案,但找不到我想要的。我有这个表格,一旦任何字段被触及,我想在ngOnInit中发出一条消息。到目前为止,如果我在任何字段中输入一个值,我可以显示一条消息(“您输入了一个值”),但我希望能够在用户触摸该字段时显示一条消息。谁能给我指出正确的方向吗?这是我的密码

提前多谢

 ngOnInit() {
 this.myForm.valueChanges.subscribe(res => {
  console.log("You entered a value");
  // Here I want to show a message ("A field has been touched") when any field of the form gets touched
 })
}
使用角度模式中的(mousedown)事件

这里是一个快速演示

还有,没有现成的表格

我想您可以利用
HostListener
,例如在您想要的dom事件中触发它,例如
focusin
。您可以将其包含在组件中,也可以创建单独的指令

样本:

@HostListener('focusin', ['$event']) onFocus(event) {
  console.log('a field was focused!')
}

希望这是一个合适的解决方法(我想它会被归类为一个?

我以前尝试过,但问题是我必须将mousedown事件添加到表单中的每个字段,而我的表单有20多个字段,因此视图中会有一堆mousedown事件,这似乎不是一个好的做法。我只是想通过组件来实现这一点,有没有一种方法可以使用您的代码创建一个指令,并将其应用于整个表单,而不是将该指令添加到每个字段中?@HenryDev看起来还好吧?非常感谢兄弟!!没问题,很高兴我能帮忙!:)