In Angular 11, What is the best approach to getting updates from properties in a shared service when using Reactive Forms?
If one component updates values in the service, how is the other component supposed to get the change without changing everything to Subjects and then patching the existing form?
I define the form:
<form [formGroup]="form" class="form-horizontal" role="form">
<div formGroupName="user">
<mat-form-field>
<input matInput formControlName="name" placeholder="Username">
</mat-form-field>
<mat-form-field>
<input matInput formControlName="fullName" placeholder="Full Name">
</mat-form-field>
</div>
createForm() {
this.form = this.fb.group({
user: this.fb.group({
name: [this.userService.currentUser.name],
fullName: [this.userService.currentUser.fullName]
}),
});
}
This is just an example, but how would I update this component's form if the value in userService.fullName changed? I could use Subject, but then I have to always set the currentUser and cannot just update sub properties and in more complex objects, things seem to get messier.
I could just use Template driven forms for this, but then the validation is more complex. Is there a better way to do this?
Similar to this question, but I have not really seen a good answer.