imagine you has a simpleService
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class DataService {
data:any={}
constructor() { }
}
a componet can be like, see that in [(ngModel)]
we are using data.nameOfPropertie
<form ngNativeValidate #f1="ngForm">
<div>
<label for="username">User Name: </label>
<input [(ngModel)]="data.username" class="form-control" type="text" name="usrname" required>
</div>
<div>
<label for="phonenum">Phone: </label>
<input [(ngModel)]="data.phonenum" class="form-control" name="phonenum" type="tel" pattern="^\d{4}-\d{3}-\d{4}$" required>
</div>
</form>
Your component.ts like
export class Form1Component {
get data()
{
return this.dataService.data;
}
set data(value)
{
this.dataService.data=value
}
constructor(private dataService:DataService){}
}
each component that inject the DataService, can access to this.dataService.data
See your forked stackblitz
NOTE1: If you choose ReactiveForms, the tecnica is similar, you share the data, and change it subscribing to myForm.valueChanges
NOTE2: For most complex form, we can use Subject and emit Value, see my response in SO question