Using FormBuilder
FormBuilder
is an injectable helper class of the @angular/forms
package of Angular. This class helps to reduce the wordiness of form creation as demonstrated in the following code:
this.movieForm = this.formBuilder.group({ movie_id: '', title: '', phase: '', category_name: '', release_year: '', running_time: '', rating_name: '', disc_format_name: '', number_discs: '', viewing_format_name: '', aspect_ratio_name: '', status: '', release_date: '', budget: '', gross: '', time_stamp: '' });
As you can see, using the group method of the FormBuilder
class, the declaration of FormGroup
and FormControl
is now implicit. We only need to have the field name followed by its default value. Here, all the default values are blank.
To use the FormBuilder
class, we first have to import it:
Import { FormGroup, FormControl, FormBuilder } from '@angular/forms';
We then inject it using the constructor of our AppComponent
component:
constructor(private...