Angular Reactive Form by Susan
Angular Reactive Forms
NG2 - MEETUP
● Susan Adelokiki
● Software Developer at Andela
● More about me at susanadelokiki.com
Forms are very effective for business applications to help users interact with software.
With Angular, we have the liberty to choose between two major form building
techniques in receiving and accepting user input
● Template-driven Forms
● Reactive Forms
Angular form-building blocks
Angular makes use of building blocks to
generate the form model. The building
blocks are used by both reactive and
template-driven forms. They are called
FormGroup and FormControl.
FormGroup and FormControl
● The FormGroup tracks values and states of the form controls. It takes the
existing formGroup instance and matches it with an HTML element.
● The Form Control tracks values and states of input elements.
● Instances of this building block create the form model
● It represents the HTML structure
● It retains form states such as dirty, valid, invalid, etc.
● It is different from the data model.
● It is the same for template-driven and reactive forms but created differently.
● For template-driven forms, Angular generates the form model
● For reactive forms, Angular does not generate the form model rather it is
generated from instances of the FormGroup and FormControl.
In Template-Driven forms, Responsibility for the form is heavy on the template
with html and data-binding.
● Uses two way data-binding with the [(ngModel)] syntax to keep user entry and
data model property in sync.
● Suitable for simple scenarios
● Angular generates Form Model by creating the instances FormGroup and
Template-Driven Form Properties
From previous image, we see that:
● It makes use of event-binding to submit
● It leverages HTML5 validation
● It leverages data-binding using ngModel
Strengths of the Template-Driven Forms
● Straightforward and simple to use
● No need to write code to copy data into the input
elements. Angular automatically generates them.
● Angular Automatically tracks the form and its
● Minimal component code
Weaknesses of Template-Driven Forms
● It does not provide the flexibility of creating form
controls, Angular directives generates those
controls based on information in the data-binding.
● Adding more & more validators to the input tag
eventually makes it unreadable
● Template-driven forms are asynchronous and
hence makes unit testing a little more complicated.
Building a Simple Reactive Form
● Import your building blocks
● Specify a property for the root formGroup
● Create an instance of the FormGroup
● Update AngularModule by adding ReactiveFormsModule to the imports array
● Bind the form to the formGroup and the input elements to the associated
formControl using the reactive form directives.
Accessing Form Model Properties
We can access the form model in two ways:
● Form Model Heirarchy
E.g To access if the firstname input element is valid:
● Referencing the form control using the Form Group’s get method.
setValue & patchValue
These methods are used to initialize or reset values in the Form model
To implement validation, we simply import the Validators from @angular/forms.
We then pass them in as a second argument to our formControl instances.
The Form Builder is useful when our forms get bigger and we have multiple form
groups. It reduces repetition and clutter by handling control creation.
● Import the FormBuilder, we can take off the FormControl import.
● Inject the FormBuilder in the constructor.
● Use the FormBuilder instance to create the formGroup.
Which is better?
Template-driven forms is useful in making small scale apps which do not
necessarily require control of the forms by the user.
Reactive forms are very useful in more complex scenarios and large scale apps
because there is flexibility on the form model and form building blocks and unit
testing can easily be done.