I have filled an array of data(options) by making a call to my database. I console.log the array (dataArray) and it shows that the array has been filled with the expected data. However, when I run the application, the select box shows none of the options from the array and only shows the default option I set in the beginning ('hello'). Any help is appreciated.
select.component.ts:
import { Component, OnInit} from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
@Component({
selector: 'app-select',
templateUrl: './select.component.html',
styleUrls: ['./select.component.css']
})
export class SelectComponent implements OnInit {
constructor(private http: HttpClient) {
}
dataArray: String[] = ['hello'];
ngOnInit() {
const httpParams = new HttpParams().set('cars', 'toyota');
console.log(this.dataArray);
let text;
this.http.get(`http://localhost/scheduler/parse.php`, { params: httpParams, responseType: 'text' }).subscribe(function (response) {
text = response;
console.log(text);
this.dataArray = JSON.parse(text.replace(/'/g, '"'));
console.log(this.dataArray);
});
}
}
select.component.html:
<mat-card style="margin-top: 25px">
<mat-form-field>
<mat-select placeholder="Cars">
<mat-option *ngFor="let data of dataArray" [value]="data">
{{data}}
</mat-option>
</mat-select>
</mat-form-field>
</mat-card>