1 回答

TA貢獻1799條經驗 獲得超8個贊
您在評論中提到了undefined錯誤。這表明您正在嘗試在數據可用之前渲染視圖。在您的元素上使用*ngIf以僅在數據可用時呈現它。
<mat-form-field *ngIf="competitionsList">
</mat-form-field>
當您使用迭代數組時,*ngFor不需要使用索引i。
替換{{ comp[i].name }}為{{ comp.name }}.
那么你的代碼將如下所示
<mat-form-field *ngIf="competitionsList">
<mat-select placeholder="Select Competition">
<mat-option *ngFor="let comp of competitionsList; let i = index" [value]="comp">
{{ comp.name }}
</mat-option>
</mat-select>
</mat-form-field>
StackBlitz 上的現場演示: https://stackblitz.com/edit/angular-material-with-angular-v5-eukdkc
編輯:您應該將響應格式化compList為competitionsList數組。
將您的subscribe()方法更改為
.subscribe(
compsList => {
this.competitionsList = Object.values(compList[0]);
});
添加回答
舉報