...

/

Two Types of Angular forms

Two Types of Angular forms

Learn about the different types of forms in Angular.

We have two options to create forms in Angular:

  • Reactive

  • Template-driven

They share some similarities, but they both provide a substantially different approach to defining and managing the form model and its data. This course focuses on reactive forms, but it’s important to be familiar with template-driven forms and understand their core aspects to build a complete picture of Angular forms.

For both form types, Angular provides classes that constitute the forms’ foundations with different levels of granularity. All these classes inherit from a base class called AbstractControl.

We need to familiarize ourselves with these concepts before proceeding and describing the main differences between template-driven and reactive forms.

  • FormControl: This is the base element. It allows managing the value and the status of an individual form control, like an input field, a drop-down, or a check box.
    const firstname = new FormControl('Francesco');
    

    The snippet above creates a new FormControl instance with a string as the initial value. For instance, if we associate this control with an input field in a form, it will immediately display the text given as an initial value.

  • FormGroup: This aggregates multiple controls (FormControls, FormArrays, or even other FormGroup instances) into a single object. Its status is given by the sum of the statuses of all its child items. Any time the value of a control changes, Angular triggers a validation cycle that returns null if no errors are found, some error objects if validation rules are broken. According to the result, the form control will be in a VALID or INVALID state. If any child control is invalid, the parent FormGroup will also be invalid.
...