我已经使用api创建了自定义选择类型组件,以填充下拉列表。有一个关于这个的例子。Stackblitz我无法为选择类型组件设置初始值。事实上,我将此代码与没有构造函数调用服务的组一起使用,因此无法解决。https://stackblitz.com/edit/ngx-formly-demo?file=src%2Fapp%2Fforms%2Fgroups%2Faddress.group.ts
使用compareWith。
跟踪选项比较算法,以在以下情况下跟踪身份检查更改。
component.html
<mat-select
[compareWith]="compareFn"
[id]="id"
[formControl]="formControl"
[formlyAttributes]="field"
[multiple]="to.multiple"
(selectionChange)="to.change && to.change(field, formControl)"
[errorStateMatcher]="errorStateMatcher"
>
<ng-container *ngFor="let item of to.options">
<mat-option [value]="item" [disabled]="item.disabled">{{
item.name
}}</mat-option>
</ng-container>
</mat-select>
component.ts
compareFn(o1: any, o2: any) {
return o1.id === o2.id;
}