Angular Material mat-select 未加载初始值

Angular Material mat-select not load the initial value

我的 mat-select 组件有问题,一切正常,但他只是在编辑时没有设置初始值...如果我更改为简单的 HTML 一切工作正常。我做错了什么?

HTML select 工作正常:

<select formControlName="category" [compareWith]="compareFn">
    <option [ngValue]="c" *ngFor="let c of categoriesService.categories$ | async"> {{c.name}}</option>
  </select>

Angular Material 未按预期工作的组件:

<mat-form-field>
    <mat-select placeholder="Category" formGroupName="category" (selectionChange)="setCategoryValueOnForm($event)"
                [compareWith]="compareFn" >
      <mat-option *ngFor="let c of categoriesService.categories$ | async" [value]="c">
        {{c.name}}
      </mat-option>
    </mat-select>
  </mat-form-field>

当使用简单的 select 元素时,我发现我的函数 compareFn 有效,但是对于 Angular Material,'y' 值始终未定义...

compareFn(x: ICategory, y: ICategory): boolean {
   console.log(x);
   console.log(y);
   console.log('CALLED COMPARE FN');
   return x && y ? x.id === y.id : x === y;
 }

gif 问题:

我做错了什么?谢谢!

我刚刚意识到我使用的是 formGroupName="category" 而正确的是 formControlName="category"...

<mat-form-field hintLabel="select one">
    <mat-select placeholder="Category" formControlName="category"
                [compareWith]="compareIds">
      <mat-option *ngFor="let c of (categoriesService.categories$ | async)" [value]="c">
        {{ c.name }}
      </mat-option>
    </mat-select>
    <mat-error>
      {{ getError('category') }}
    </mat-error>
  </mat-form-field>

.ts

compareIds(category1: ICategory, category2: ICategory): boolean {
    return category1 && category2 ? category1.id === category2.id : category1 === category2;
}