Angular 5 + Material Design:<mat-select>如何设置默认值?

时间:2017-12-01 11:24:19

标签: mongodb typescript material-design angular-material2 angular5

 <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...)

opened mat-select

Here is my .ts. The important line is "this.resource.categoryName = this.subdoc['name'];". There i set the "resource.categoryName. If i try to console.log(this.resource.categoryName), then i get the name of the category...

在.ts中创建一个变量并确保它是一个字符串。之后,使用默认类别的ID声明变量。 (在我的情况下,它只使用ID,而不是简单的名称) - 感谢Simeon,Hamid和其他线程关于这个问题。

1 个答案:

答案 0 :(得分:1)

我看不到您的组件文件,但问题可能是您的resource.categoryName在组件初始化时没有设置值。尝试在* .component.ts文件中手动设置。

相关问题