Choosing between reactive and template-driven forms helps you decide how to build and manage forms in Angular. Each way has its own style and benefits.
Reactive forms vs template forms decision in Angular
Template-driven form example: <form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)"> <input name="email" ngModel required email /> <button type="submit">Submit</button> </form> Reactive form example: import { FormGroup, FormControl, Validators } from '@angular/forms'; myForm = new FormGroup({ email: new FormControl('', [Validators.required, Validators.email]) }); <form [formGroup]="myForm" (ngSubmit)="onSubmit()"> <input formControlName="email" /> <button type="submit">Submit</button> </form>
Template-driven forms use directives in HTML to create and manage forms.
Reactive forms use explicit form model objects in TypeScript for better control.
<form #formRef="ngForm"> <input name="username" ngModel required /> </form>
myForm = new FormGroup({ username: new FormControl('', Validators.required) }); <form [formGroup]="myForm"> <input formControlName="username" /> </form>
This Angular component shows both reactive and template-driven forms side by side. Each form has email and password fields with validation. The reactive form uses FormGroup and FormControl in TypeScript. The template-driven form uses ngModel and template references in HTML. Both forms disable the submit button until valid and show error messages when touched and invalid.
import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators } from '@angular/forms'; @Component({ selector: 'app-login', template: ` <h2>Reactive Form Example</h2> <form [formGroup]="loginForm" (ngSubmit)="submit()"> <label for="email">Email:</label> <input id="email" formControlName="email" /> <div *ngIf="email.invalid && email.touched" style="color:red"> Email is required and must be valid. </div> <label for="password">Password:</label> <input id="password" type="password" formControlName="password" /> <div *ngIf="password.invalid && password.touched" style="color:red"> Password is required. </div> <button type="submit" [disabled]="loginForm.invalid">Login</button> </form> <h2>Template-driven Form Example</h2> <form #templateForm="ngForm" (ngSubmit)="submitTemplate(templateForm)"> <label for="email2">Email:</label> <input id="email2" name="email" ngModel required email #email2="ngModel" /> <div *ngIf="email2.invalid && email2.touched" style="color:red"> Email is required and must be valid. </div> <label for="password2">Password:</label> <input id="password2" name="password" type="password" ngModel required #password2="ngModel" /> <div *ngIf="password2.invalid && password2.touched" style="color:red"> Password is required. </div> <button type="submit" [disabled]="templateForm.invalid">Login</button> </form> ` }) export class LoginComponent { loginForm = new FormGroup({ email: new FormControl('', [Validators.required, Validators.email]), password: new FormControl('', Validators.required) }); get email() { return this.loginForm.get('email')!; } get password() { return this.loginForm.get('password')!; } submit() { if (this.loginForm.valid) { alert('Reactive form submitted: ' + JSON.stringify(this.loginForm.value)); } } submitTemplate(form: any) { if (form.valid) { alert('Template-driven form submitted: ' + JSON.stringify(form.value)); } } }
Reactive forms give you more power and flexibility for complex forms.
Template-driven forms are easier for simple forms and quick setups.
Both forms support validation and user feedback.
Reactive forms use explicit form models in TypeScript for control.
Template-driven forms use directives in HTML for simplicity.
Choose based on form complexity and your preference for code style.
Practice
Solution
Step 1: Understand form complexity
Simple forms with minimal logic are easier to manage directly in HTML templates.Step 2: Match form type to complexity
Template-driven forms are designed for this use case, relying on Angular directives in HTML.Final Answer:
Template-driven forms -> Option CQuick Check:
Simple forms = Template-driven forms [OK]
- Confusing reactive forms as simpler
- Thinking both are equally suited for simple forms
- Believing Angular doesn't support template forms
Solution
Step 1: Recall reactive form initialization
Reactive forms use FormGroup and FormControl classes to define form structure in TypeScript.Step 2: Identify correct syntax
this.form = new FormGroup({ name: new FormControl('') }); correctly creates a FormGroup with a FormControl named 'name'.Final Answer:
this.form = new FormGroup({ name: new FormControl('') }); -> Option AQuick Check:
Reactive form init uses FormGroup and FormControl [OK]
- Using NgForm which is for template forms
- Confusing FormBuilder usage without calling group()
- Trying to initialize reactive forms like template forms
console.log(this.form.value) after setting this.form.controls['email'].setValue('test@example.com')?
this.form = new FormGroup({ email: new FormControl('') });Solution
Step 1: Understand form initialization
The form is initialized with an 'email' control starting as an empty string.Step 2: Setting control value
Calling setValue on 'email' updates its value to 'test@example.com'.Step 3: Checking form value output
Logging form.value returns an object with the current control values, so it shows the updated email.Final Answer:
{ email: 'test@example.com' } -> Option AQuick Check:
Reactive form values reflect control updates [OK]
- Expecting empty string after setValue call
- Confusing controls property access
- Assuming form.value is undefined before submit
<form #myForm="ngForm"> <input name="username" ngModel> <button type="submit">Submit</button> </form>
Solution
Step 1: Check input element attributes
The input has both 'name' and 'ngModel' directives, which are required for template-driven forms.Step 2: Check form tag
The form uses '#myForm="ngForm"' which correctly references the Angular form directive.Final Answer:
No error, the form is correct -> Option BQuick Check:
Template form with name and ngModel is valid [OK]
- Forgetting name attribute on inputs
- Confusing ngForm with reactive form syntax
- Assuming form tag needs extra directives
Solution
Step 1: Analyze form complexity and requirements
Complex forms with dynamic validation and conditional fields require programmatic control.Step 2: Match form approach to needs
Reactive forms provide APIs in TypeScript to add/remove controls and validations dynamically.Step 3: Compare with template-driven forms
Template-driven forms are less suited for complex logic as they rely mostly on HTML directives.Final Answer:
Reactive forms, because they allow better control and dynamic logic in TypeScript -> Option DQuick Check:
Complex dynamic forms = Reactive forms [OK]
- Choosing template forms for complex dynamic needs
- Assuming both approaches handle dynamic validation equally
- Ignoring benefits of TypeScript control in reactive forms
Ready to prove it?
Pass the test and this pattern moves unknown
