Forms, routing and HTTP · core
Reactive, template-driven and typed forms with validators
Reactive forms keep the model in the class and are synchronous and typed. Know value versus getRawValue() and how to write cross-field and async validators.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Template-driven:
ngModelin the template; the model is built by directives and updates asynchronously. Quick for small forms, harder to test and build dynamically. - Reactive:
FormGroup,FormControl,FormArrayin the class; synchronous access, strongly typed since v14,valueChangesas an Observable. form.valueleaves out disabled controls. Useform.getRawValue()to include them, so a disabledpanfield is not lost.fb.groupgivesFormControl<string | null>becausereset()setsnullby default.form.valueisPartial<...>because disabled controls are missing.- A cross-field validator is a
ValidatorFnon the group: it returnsnullwhen valid or an error object such as{ mismatch: true }. - An async validator is an
AsyncValidatorFn, for exampleusernameFree(api); the control isPENDINGwhile it runs. - Debounce async validators so you do not call the server on every key.
The red pen
Common traps: mistake → fix
Mistake: form.value always has every field
Fix: disabled controls are missing.
Mistake: Template-driven forms are synchronous
Fix: the model updates asynchronously.
Easy to hard
Questions interviewers ask, easy to hard
- Reactive or template-driven forms: how do you choose?
- Why is my disabled field missing from form.value?
- How do you write a "passwords match" validator?
- Why is the control type
string | null?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.