<mat-form-field>
<mat-select [formControl]="form.controls['defaultCategory']" [(ngModel)]="resource.categoryName" >
<mat-option *ngFor="let category of categories | async" [value]="category._id" >
{{ category.name }}
</mat-option>
</mat-select>
</mat-form-field>
我试了很多东西。文档,关于github的一些错误报告仍然无法解决我的问题。
我只想预设默认值...
默认值存储名为:resource.categoryName的变量。选项来自mongodb的集合,这些集合存储在类别[]。
中This picture shows the closed mat-select (no default value shown, but it should...)
在.ts中创建一个变量并确保它是一个字符串。之后,使用默认类别的ID声明变量。 (在我的情况下,它只使用ID,而不是简单的名称) - 感谢Simeon,Hamid和其他线程关于这个问题。
答案 0 :(得分:1)
我看不到您的组件文件,但问题可能是您的resource.categoryName在组件初始化时没有设置值。尝试在* .component.ts文件中手动设置。