[英]How to detect input field value changes in angular with each value insertion?
How to detect input field value changes immediately without pressing enter button in angular?如何在不按 angular 中的回车按钮的情况下立即检测输入字段值的变化?
I was trying to trigger a function on a value change of input field in Angular. Initially I used Keypress event, that was detecting the insertion the input field correctly, but even I used backspace to remove any character from the value, it didn't trigger that function, which means that these changes went unnoticed.我试图在 Angular 中输入字段的值更改时触发 function。最初我使用 Keypress 事件,它正确检测到输入字段的插入,但即使我使用退格键从值中删除任何字符,它也没有触发 function,这意味着这些更改未被注意到。 I was expecting that it would trigger that event on the each change or update of the value.
我期待它会在每次更改或更新值时触发该事件。
In HTML HTML
<input (input)="type($event)" type="text" />
In TS在TS
type(event) {
console.log(event.target.value);
}
In HTML HTML
<input type="text" [ngModel]="mymodel" (ngModelChange)="valuechange($event)" />
In TS在TS
mymodel:any
valuechange(newValue) {
this.mymodel = newValue;
console.log(newValue)
}
you can use [(ngModel)].你可以使用 [(ngModel)]。 I suggest you "split" the "bannana sintax"
我建议你“拆分”“bannana sintax”
<input matInput placeholder="Word"
[ngModel]="search"
(ngModelChange)="search=$event;doSomething($event)">
doSomething(value:string)
{
console.log(value)
}
Another ways can be另一种方法可以是
<!--see that the event "input" return a "generic event"
so you use $event.target.value to "reach" the value-->
<input matInput placeholder="Word"
[(ngModel)]="search"
(input)="doSomething($event.target.value)">
Or或者
<input matInput placeholder="Word"
[(ngModel)]="search"
(ngModelChange)="doSomething($event)">
By using two-binding and ngModelChange event, worked for me to detect all changes.通过使用两个绑定和 ngModelChange 事件,我可以检测所有更改。 Sample code:
示例代码:
<input matInput placeholder="Word"
[(ngModel)]="search"
(ngModelChange)="filterTbl()"
matTooltip="Filter Result">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.