I have a primary form within a service named "MainService" (the actual form is much lengthier). Here is an overview-
export class MainService {
this.mainForm = this.formBuilder.group({
A: ['', Validators.required],
B: '',
C: '',
D: this.formBuilder.array([]),
E: this.formBuilder.array([]),
F:'',
G: this.formBuilder.array([]),
H: this.formBuilder.array([]),
I: this.formBuilder.array([]),
});
}
}
Various components access the form's values and modify the form by importing the service. The template appears like this-
<mat-form-field>
<input matInput [formControl]="MainService.A">
<mat-label></mat-label>
</mat-form-field>
Is this the most effective approach for sharing the form among different components? Each component corresponds to a distinct form control in the complete form, hence they need to refer to the same form.
For example, imagine a student filling out various details about an article they read (book title, number of pages, authors, topics, etc.). Each detail of the book serves as a form control within the main form, with each individual component responsible for updating a specific form control (e.g. one component managing the name input, another dealing with author information).
Thank you!