1

I know that you can use the valueChanges observable in Angular Reactive Forms to detect if a value in the input field has already changed:

inputControl.valueChanges.subscribe(() => {
    // fires when the input value has actually changed
});

However, is there a way to subscribe to an observable of when the user has pressed a key, before the value changes in the input?

I need to do this so I can apply dynamic validation, for example:

  1. User presses a key
  2. I need to know what that key is before I apply validation
  3. Validation then works on the changed value of the input

1 Answer 1

2

is there a way to subscribe when the user has pressed a key, before the value changes in the input?

onkeyup event :

 <input formControlName="yourcontrol" (keyup)="onKeyUp($event)">

EDIT :

You need subscribe to event keyup on your AbstractControl elememnt.

see the stackblitz

You could also create a directive to access your element using ElementRef regarding what your requirements are.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I want to subscribe to keypresses on the form control only, not on the whole document.
Thanks again, but the example isn't a subscription to an observable (e.g. like in the valueChanges example), which is what I'm looking for. I'll try to make my question more clear on that. If it's definitely not possible that way, please include that your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.