每次按键时的 Angular 2 更改事件 [英] Angular 2 change event on every keypress

查看:21
本文介绍了每次按键时的 Angular 2 更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有在输入的焦点发生变化后才会调用 change 事件.我怎样才能使事件在每次按键时触发?

The change event is only called after the focus of the input has changed. How can I make it so that the event fires on every keypress?

<input type="text" [(ngModel)]="mymodel" (change)="valuechange($event)" />
{{mymodel}}

顺便说一下,每次按键都会改变第二个绑定.

The second binding changes on every keypress btw.

推荐答案

我刚刚使用了事件输入,效果很好,如下所示:

I just used the event input and it worked fine as follows:

在 .html 文件中:

in .html file :

<input type="text" class="form-control" (input)="onSearchChange($event.target.value)">

在 .ts 文件中:

onSearchChange(searchValue: string): void {  
  console.log(searchValue);
}

这篇关于每次按键时的 Angular 2 更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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