使用 (focus) 检索输入的值
Retrieve value of a input with (focus)
我的输入是这样的:
<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>
我的方法是:
focusIn(event: FocusEvent) {
console.log(event.detail);
}
focusOut(event: FocusEvent) {
console.log(event.detail);
}
基本上我想在写完后立即检索输入的值,这就是我使用 focusOut
.
的原因
然而我一直检索到 0,有什么原因吗?
以及如何在这两个焦点之间传递数据?
谢谢!
从 post 来看,我相信您的主要意图是实际知道用户何时完成输入字段中的某些更改,然后使用该值。
您可以使用 (change)
事件来了解字段中何时发生了变化。
仅当您将焦点移出输入并且输入值发生变化时才会触发。所以你真的不必听所有其他事件。
如果即使输入的值没有变化,您仍然希望收到通知,使用 (blur)
事件将是理想的选择。
Here's a Working Sample StackBlitz for your ref.
您正在寻找模糊方法。
在组件中的用法:
onBlur($event) {
console.log($event.target.value);
}
标记中的用法
<input type="text" (blur)="onBlur($event)" >
我的输入是这样的:
<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>
我的方法是:
focusIn(event: FocusEvent) {
console.log(event.detail);
}
focusOut(event: FocusEvent) {
console.log(event.detail);
}
基本上我想在写完后立即检索输入的值,这就是我使用 focusOut
.
然而我一直检索到 0,有什么原因吗?
以及如何在这两个焦点之间传递数据?
谢谢!
从 post 来看,我相信您的主要意图是实际知道用户何时完成输入字段中的某些更改,然后使用该值。
您可以使用 (change)
事件来了解字段中何时发生了变化。
仅当您将焦点移出输入并且输入值发生变化时才会触发。所以你真的不必听所有其他事件。
如果即使输入的值没有变化,您仍然希望收到通知,使用 (blur)
事件将是理想的选择。
Here's a Working Sample StackBlitz for your ref.
您正在寻找模糊方法。
在组件中的用法:
onBlur($event) {
console.log($event.target.value);
}
标记中的用法
<input type="text" (blur)="onBlur($event)" >