Site icon Tutorial

Setting Up the Form

Reactive and template-driven forms both use a form model to track value changes between Angular forms and form input elements. The examples below show how the form model is defined and created.

Setup in reactive forms

Here is a component with an input field for a single control implemented using reactive forms.

import { Component } from ‘@angular/core’;

import { FormControl } from ‘@angular/forms’;

@Component({

selector: ‘app-reactive-favorite-color’,

template: `

Favorite Color: <input type=”text” [formControl]=”favoriteColorControl”>

`

})

export class FavoriteColorComponent {

favoriteColorControl = new FormControl(”);

}

The source of truth provides the value and status of the form element at a given point in time. In reactive forms, the form model is source of truth. The form model in the above example is the FormControl instance.

With reactive forms, the form model is explicitly defined in the component class. The reactive form directive (in this case, FormControlDirective) then links the existing form control instance to a specific form element in the view using a value accessor (instance of ControlValueAccessor).

Setup in template-driven forms

Here is the same component with an input field for a single control implemented using template-driven forms.

import { Component } from ‘@angular/core’;

@Component({

selector: ‘app-template-favorite-color’,

template: `

Favorite Color: <input type=”text” [(ngModel)]=”favoriteColor”>

`

})

export class FavoriteColorComponent {

favoriteColor = ”;

}

In template-driven forms, the source of truth is the template.

The abstraction of the form model promotes simplicity over structure. The template-driven form directive NgModel is responsible for creating and managing the form control instance for a given form element. It is less explicit, but you no longer have direct control over the form model.

Data flow in forms

When building forms in Angular, it’s important to understand how the framework handles data flowing from the user or from programmatic changes. Reactive and template-driven forms follow two different strategies when handling form input. The data flow examples below begin with the favorite color input field example from above, and they show how changes to favorite color are handled in reactive forms compared to template-driven forms.

Data flow in reactive forms

As described above, in reactive forms each form element in the view is directly linked to a form model (FormControl instance). Updates from the view to model and model to view are synchronous and not dependent on the UI rendered. The diagrams below use the same favorite color example to demonstrate how data flows when an input field’s value is changed from the view and then from the model.

The steps below outline the view to model data flow.

The steps below outline the model to view data flow.

Data flow in template-driven forms

In template-driven forms, each form element is linked to a directive that manages the form model internally. The diagrams below uses the same favorite color example to demonstrate how data flows when an input field’s value is changed from the view and then from the model.

The steps below outline the view to model data flow.

 

The steps below outline the model to view data flow.

 

 

Exit mobile version