我遇到了mat-select组件的问题,一切正常,但他只是在编辑时没有设置初始值...如果我改为简单的HTML一切正常 . 我做错了什么?
HTML选择工作正常:
<select formControlName="category" [compareWith]="compareFn">
<option [ngValue]="c" *ngFor="let c of categoriesService.categories$ | async"> {{c.name}}</option>
</select>
角度材质组件无法按预期工作:
<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:
我做错了什么?谢谢!
1 回答
我刚刚意识到我正在使用formGroupName =“category”,当正确的是formControlName =“category”时......
.TS