That's a main purpose of @Input()
decorator in angular:
In your country-state.component.ts
add Input
to your import and use that decorator as mentioned below:
import { Component, OnInit, Input } from '@angular/core';
...
@Component({
selector: 'app-country-state',
templateUrl: './country-state.component.html',
styleUrls: ['./country-state.component.css']
})
export class CountryStateComponent implements OnInit {
countries: Country[];
states: State[];
@Input()
studentForm; // <-- This
...
}
Then in your app.component.ts change your child tag to:
<app-country-state [studentForm]="studentForm"></app-country-state>
After that and in your country-state.component.html
add form
tag like :
<form [formGroup]="studentForm">
<label>Country:</label>
<select formControlName="countryId" (change)="onSelect($event.target.value)">
<option [value]="0">--Select--</option>
<option *ngFor="let country of countries" [value]="country.id">{{country.name}}</option>
</select>
<br/><br/>
<label>State:</label>
<select formControlName="stateId">
<option [value]="0">--Select--</option>
<option *ngFor="let state of states " value= {{state.id}}>{{state.name}}</option>
</select>
</form>
Thus, you wont create a new formgroup
instance but you'll be using the parent's one.