I have an edit form with mat-select dropdown list. I want to set the selected item in the dropdown when i go to the edit form .For this im calling a service to get the Current value.
my html:
<mat-form-field appearance="outline">
<mat-select formControlName="organisationUnit" placeholder="Organisation Unit" [(value)]="selected">
<mat-option *ngFor="let unit of orgUnits" [value]="unit.id" >
{{unit.name}}
</mat-option>
</mat-select>
</mat-form-field>
My ts:
ngOnInit() {
this.setupFirstFormGroup();
this.setupSecondFormGroup();
this.firstFormGroup.get('status').setValue(true);
this._route.params.subscribe(params => {
this.tenantAdminService.getUser(params.id).subscribe(res => {
this.firstFormGroup.get('first_name').setValue(res['first_name']);
this.firstFormGroup.get('last_name').setValue(res['last_name']);
this.tenantAdminService.getOrganizationUnit(res['organizationUnit']).subscribe(res => {
console.log("ORGUNIT", res);
this.selected = res['id'];
this.firstFormGroup.get('organisationUnit').setValue(res['id']);
});
});
});
}
Currently its not setting the value in the dropdown and its empty. Its printing out the value but the selected option doesnt show as selected