I was able to populate dropdowns when using template driven forms but now in material reactive form i am unable to do so. I am trying to populate country dropdown so that i might be able to call a onstatechange event later on to populate states according to the selected country. Here is my code
<div fxLayout="row wrap" class="pt-4">
<mat-form-field appearance="outline" fxFlex="33">
<mat-label>Select a Country</mat-label>
<mat-select [compareWith]="compareThese" formControlName="countryCtrl" (selectionChange)="loadState($event)">
<mat-option *ngFor="let item of countriesData$ | async" [value]="item.id">
{{ item.name }}
</mat-option>
</mat-select>
<mat-icon matSuffix class="disabled-text">account_circle</mat-icon>
</mat-form-field>
<mat-form-field appearance="outline" fxFlex="34" class="px-8">
<mat-label>Select a State</mat-label>
<mat-select [compareWith]="compareThese" formControlName="stateCtrl" (selectionChange)="loadCity($event)">
<mat-option *ngFor="let item of filteredStates$ | async" [value]="item.id">
{{ item.name }}
</mat-option>
</mat-select>
<mat-icon matSuffix class="disabled-text">account_circle</mat-icon>
</mat-form-field>
<mat-form-field appearance="outline" fxFlex="33">
<mat-label>Select a City</mat-label>
<mat-select [compareWith]="compareThese" formControlName="cityCtrl">
<mat-option *ngFor="let ele of filteredCities$ | async" [value]="ele.id">
{{ ele.name }}
</mat-option>
</mat-select>
<mat-icon matSuffix class="disabled-text">account_circle</mat-icon>
</mat-form-field>
</div>
Here is how my JSON looks
ts file code
export class ManualFormComponent implements OnInit {
countryCtrl: any;
stateCtrl: any = {};
cityCtrl: any = {};
manualOrderForm: FormGroup;
zoom: any;
countriesData$: Observable<ICountry[]>;
filteredStates$: Observable<IState[]>;
filteredCities$: Observable<ICity[]>;
constructor(
private _cityStateService: CountryStateCityService,
private _fb: FormBuilder) {
this.manualOrderForm = this._fb.group({
countryCtrl: [[], Validators.required],
stateCtrl: [[], Validators.required],
cityCtrl: [[], Validators.required],
});
}
submit() {
console.log(this.manualOrderForm);
}
compareThese(o1: any, o2: any) {
return o1 === o2 ? true : false;
}
ngOnInit() {
this._cityStateService.getCountriesStatesCity().subscribe(val => {
console.log(val);
this.countriesData$ = val[0];
console.log(this.countriesData$);
});
}
}
Interface for Country
import { IState } from './iState';
export interface ICountry {
name: string;
id: string;
states: IState[];
}
State
import { ICity } from './iCity';
export interface IState {
cities: ICity[];
id: string;
name: string;
countryId: string;
}

export class DataService { constructor() { } getData(): Observable<{name: string, id: string}[]> { return of([ { id: '5da06f27b2dcbf00125c908a', name: 'Pakitan', states: [ { name: 'Value 1', id: 'val1' }, { name: 'Value 2', id: 'val2' }, { name: 'Value 3', id: 'val3' }, { name: 'Value 4', id: 'val4' }, { name: 'Value 5', id: 'val5' } ] } ]); } }