Choosing an Approach
Reactive Forms
Provide direct, explicit access to the underlying form object model. More robust, scalable, reusable, and testable.
Template-Driven Forms
Rely on directives in the template to create and manipulate the underlying object model. Useful for simple forms.
Key Differences
| Feature | Reactive | Template-driven |
|---|---|---|
| Form model setup | Explicit, created in component class | Implicit, created by directives |
| Data model | Structured and immutable | Unstructured and mutable |
| Data flow | Synchronous | Asynchronous |
| Form validation | Functions | Directives |
Common Building Blocks
Both approaches share underlying building blocks:FormControl
Tracks the value and validation status of an individual form control.packages/forms/src/model/form_control.ts
FormGroup
Tracks the value and validity state of a group of FormControl instances.packages/forms/src/model/form_group.ts
FormArray
Tracks the value and validity state of an array of FormControl, FormGroup, or FormArray instances.packages/forms/src/model/form_array.ts
Form State Properties
All form controls track state that helps you understand the user interaction:Value State
Value State
value: The current value of the controlvalueChanges: Observable that emits every time the value changes
Validation State
Validation State
valid: Whether the control passes all validatorsinvalid: Whether the control fails any validatorserrors: Object containing validation errors, ornullstatusChanges: Observable that emits status changes
User Interaction State
User Interaction State
pristine: User has not changed the value (opposite ofdirty)dirty: User has changed the valueuntouched: User has not visited the control (opposite oftouched)touched: User has visited the control (blur event)
Disabled State
Disabled State
disabled: Control is disabled and excluded from validationenabled: Control is enabled
FormBuilder
TheFormBuilder service provides syntactic sugar to reduce boilerplate:
packages/forms/src/form_builder.ts
Data Flow
View to Model
User types into an input element. The input element emits an “input” event with the latest value.
Control Value Accessor
The ControlValueAccessor listening for events on the form input element immediately relays the new value to the FormControl instance.
Next Steps
Reactive Forms
Learn about explicit form control with reactive forms
Template-Driven Forms
Learn about template-driven forms with ngModel
Form Validation
Add validation to your forms
