Question presented to candidate: "We have a two-field newsletter box built with ngModel, and a twelve-step onboarding wizard. What is the difference between template-driven and reactive forms, and which would you use for each?"
What a strong answer should cover:
- Both are built on the same classes (
FormControl,FormGroup); the difference is who creates the form model: the template (ngModel+name) or the component class (new FormGroup(...)). - Template-driven (
FormsModule): two-way binding to plain fields, validation through attributes, controls registered asynchronously (a microtask after the view is created). - Reactive (
ReactiveFormsModule): the model exists synchronously in the constructor, is strongly typed since v14, exposesvalueChanges/statusChanges/eventsObservables, and is easy to unit test without a DOM. - Dynamic structure (adding fields, swapping validators, cross-field rules) is natural in reactive forms and awkward in templates.
- Pick by complexity: template-driven for small, static forms; reactive for anything dynamic, large or heavily validated. Mention Signal Forms (stable in v22) as the new third option.
Clarifying questions expected:
- "Is the structure of the form known up front, or does it change at runtime?"
- "Do we need to unit test validation logic without rendering the component?"
Code / implementation expected: Optional β the same field written both ways, ideally showing the template-driven control appearing a tick later.