There are two elements in this code snippet that are referencing the same variables:
<app-country [studentForm]="studentForm"></app-country>
<app-state [studentForm]="studentForm"></app-state>
<br/><br/>
<app-country [studentForm]="studentForm"></app-country>
<app-state [studentForm]="studentForm"></app-state>
Both components have access to the same property of the studentForm class. To have each component group filled separately, you need to make some changes:
app.component.ts:
this.studentForm = new FormGroup({
'studentName':new FormControl(''),
'countryId1': new FormControl(''),
'stateId1': new FormControl(''),
'countryId2': new FormControl(''),
'stateId2': new FormControl('')
})
app.component.html:
<app-country [studentForm]="studentForm" [id]="'countryId1'"></app-country>
<app-state [studentForm]="studentForm" [id]="'stateId1'" [countryId]="'countryId1'"></app-state>
<br/><br/>
<app-country [studentForm]="studentForm" [id]="'countryId2'"></app-country>
<app-state [studentForm]="studentForm" [id]="'stateId2'" [countryId]="'countryId2'"></app-state>
In your country and state components, make sure to use countryId1/countryId2 and stateId1/stateId2 respectively (and modify country and state components to use the 'id' parameter).
UPDATE
In country.component.ts, add:
@Input() id: string;
In state.component.ts, add:
@Input() id: string;
@Input() countryId: string;
get states(): State[]{
var val = this.studentForm.controls[this.countryId].value;
return this.selectService.filterStates(val);
};
In country/state.component.html, change to:
<select [formControlName]="id">...
In select.service.ts, change:
filterStates(countryId){
if(!countryId) return null;
return this.getStates().filter((item) => item.countryid == countryId);
}