I seem to be facing an issue as I am unable to populate the md-option with the data retrieved from an API. It feels like there might be something missing in my code.
Below is the service.ts file where I make the API call and attempt to fetch the data:
getCars(){
this.http.get(this.rootURL+'/car/getallcars')
.toPromise()
.then(res => this.carList = res as Cars[]);
}
This is how the API response looks like:
{
"id": "b49981fc-730e-49fc-b5e4-0159f4b42c9d",
"brand": "Mercedes",
"model": "G-Klasse",
"mileage": 19000,
"isAvailable": true
}
In my HTML file, it's structured like this:
<mat-form-field appearance="fill">
<mat-label>Field</mat-label>
<mat-select name="myField" #brand="ngModel [(ngModel)]="service.formData.brand">
<mat-option *ngFor ="let car of carList" [value]="car.id" >{{car.brand}}</mat-option>
</mat-select>
The challenge lies in figuring out how to correctly fetch the elements from the API within the component.ts file in order to populate the mat-option successfully.