Angular Material mat-select, 做好列表之后,需要设置一个当前值,挺麻烦的,经过一番研究,网上查到了方法:
https://stackoverflow.com/questions/51989366/angular-objects-as-select-option-values
就是设置mat-select的compareWith属性,其值是一个方法名。
HTML:
<mat-form-field appearance="fill">
<mat-label>站点</mat-label>
<mat-select [(value)]="device.detectUnit" [compareWith]="setUnit">
<mat-option *ngFor="let detectUnit of detectUnits" [value]="detectUnit" >
{{ detectUnit.name }}
</mat-option>
</mat-select>
</mat-form-field>
TS:
setUnit ( option:DetectUnit, value:DetectUnit ) : boolean {
return option.id === value.id;
}
效果:
问题解决。