Creating elegant reactive forms
So far, we have been using the FormGroup
, FormControl
, and FormArray
classes to create our forms, as follows:
loginForm = new FormGroup({   username: new FormControl('', Validators.required),   password: new FormControl('', [     Validators.required,     Validators.minLength(6)   ]) });
The previous way, however, constitutes a lot of noise, especially in forms that contain many controls. Alternatively, we can use an Angular service called FormBuilder
to take away a lot of that noise. We can import FormBuilder
from the @angular/forms
npm package, inject it into the constructor
of the component, and rewrite loginForm
as follows:
constructor(private builder: FormBuilder) { } private buildForm() {   this.loginForm = this.builder.group({     username: ['...