I am having problem with Angular's (keypress). I want to store event.target.value as variable but if I write 'pizza' in the input field, it stores 'pizz'. How can I store the 'pizza' anyway?
Thanks for the help.
HTML:
<input type="text" (keypress)="onKeypressEvent($event)" />
Typescript:
storedValue: string = '';
onKeypressEvent(event: any) {
console.log(event);
console.log(event.target.value);
this.storedValue = event.target.value;
if (event.key === 'Enter') {
this.onSubmit();
}
}
Working example on StackBlitz: https://stackblitz.com/edit/angular-ivy-ddyqbx?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts
SOLVED
I added two way binding to my input field and it solved the problem. Maybe this is not "the best" practice, but it solved the problem.
<input type="text" (input)="keyValue(event)" (keypress)="onKeypressEvent($event)" />
onKeypressEventOccupation(event: any) {
if (event.key === 'Enter') {
this.onSubmit();
}
}
keyValue(event: any) {
this.storedValue = event.target.value;
}