我使用 Angular Material
mat-select
组件进行了选择倍数。我的选项在下拉列表中正确显示,但所选值未保留在 UI 中。例如,在页面加载时 locationNameSelectedItems
返回 -
[{
id: 60
itemName: "Main Location - 1100 Superior Road"
}]
<mat-form-field>
<mat-select [(ngModel)]="locationNameSelectedItems" formControlName="locationName" multiple>
<mat-option *ngFor="let location of locationNameDropdownList" [value]="location.id">{{location.itemName}}</mat-option>
</mat-select>
</mat-form-field>
如何确保显示正确的选择?
我有类似的情况,我想用 mat-select 和 mat-option 进行双向绑定,我用以下方法解决了。 这可能不是一个准确的解决方案,但这可能对您有帮助。
在 TypeScript 文件中提及模型对象
category: CategoryModel = {
categoryId: 1,
// other fields(if any)
};
然后在 HTML 模板中使用如下所示的片段代码,
<mat-select name="categoryId" [(ngModel)]="category.categoryId" required>
<mat-option *ngFor="let categ of categories" [value]="categ.categoryId">
{{ categ.categoryTitle }}
</mat-option>
</mat-select>