简体   繁体   English

使用(焦点)检索输入的值

[英]Retrieve value of a input with (focus)

My Input looks like that :我的输入看起来像这样:

<mat-form-field>
  <input 
    matInput 
    placeholder="A blablabla" 
    required 
    (focus)="focusIn($event)" 
    (focusout)="focusOut($event)" 
    [(ngModel)]="A.txt1" 
    (ngModelChange)="onChange()"
    (keypress)="numberOnlyMin($event)">
</mat-form-field>

My methods are :我的方法是:

focusIn(event: FocusEvent) {
  console.log(event.detail);
}

focusOut(event: FocusEvent) {
  console.log(event.detail);
}

Basically I want to retrieve the value of my input right after I finish writing, that is why I use focusOut .基本上我想在写完后立即检索输入的值,这就是我使用focusOut

However I retrieve 0 all the time, is there any reason why ?但是我一直检索0,有什么原因吗?

And how to pass data between those 2 focus?以及如何在这两个焦点之间传递数据?

Thanks !谢谢 !

Judging from the post, I believe your main intent is to actually know when the user has finished changing something in the input field and then use the value.从帖子来看,我相信您的主要意图是真正知道用户何时完成了输入字段中的某些更改,然后使用该值。

You could use the (change) event in order to know when something has changed in the field.您可以使用(change)事件来了解字段中的某些内容何时发生了变化。

This will fire only once you've focused out of the input and there's a change in the input value.只有在您关注输入并且输入值发生变化时才会触发。 So you don't really have to listen to all those other events.所以你真的不必听所有其他事件。

If you still want to be notified even if there's no change in the value of the input, using the (blur) event would be ideal.如果您仍然希望在输入值没有变化的情况下收到通知,则使用(blur)事件将是理想的选择。


Here's a Working Sample StackBlitz for your ref.这是供您参考的工作示例 StackBlitz

You are looking for the blur-method.您正在寻找模糊方法。

Usage in Component:在组件中的使用:

  onBlur($event) {
    console.log($event.target.value);
  }

Usage in Markup在标记中的使用

<input type="text" (blur)="onBlur($event)" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM