What are async validators and when do you need one?
Model answer
An async validator returns an Observable or Promise, and is used for checks that need the server, such as "is this username taken?". While it runs the control status is pending.
To avoid a request on every key press, debounce with a timer and switchMap, or update only on blur (updateOn: 'blur'). Also handle errors: if the check fails because of the network, do not block the user forever.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is the difference between template-driven and reactive forms?
- How do you add validation to a reactive form?
- How do you write a custom validator?
- What are FormArray and dynamic forms?
- What is the difference between valueChanges and statusChanges, and how do you use them safely?
- What do touched, dirty and pristine mean?