【发布时间】:2018-05-17 21:12:23
【问题描述】:
在我的 Angular 5 应用程序中,我有一个输入框,当按下回车键时,它会调用一个返回数组的 API。然后在 mat-select 中使用该数组来填充 mat-options。第一项被自动选中,但显示值永远不会更新。
换句话说,当 mat-select 填充动态数据时,对其 ngModel 的赋值不会反映在其显示值中。
component.html:
<mat-form-field>
<input matInput placeholder="Order ID" [(ngModel)]="orderID (keydown)="handleKeyDown($event)">
</mat-form-field>
<mat-form-field>
<mat-select placeholder="Product ID" (change)="showProductDetails(productID)" [(ngModel)]="productID" >
<mat-option *ngFor="let product of products" [value]="product.id"> {{ product.name }} </mat-option>
</mat-select>
</mat-form-field>
component.ts:
orderID = '';
products = [];
handleKeyDown(event: any) {
if (event.keyCode === 13) { this.getProductIDs(); }
}
getProductIDs(){
... /~ Call API and populate "products" ~~/
// Update the mat-select's ngModel - value is updated but display is not
productID = products[0].id;
}
showProductDetails(productID){ /~~ Do some stuff }
【问题讨论】: