更改垫选择默认值

时间:2020-02-21 15:24:13

标签: angular-material

即使我以适当的方式设置value属性,我也无法更改默认值或我的垫选择项。

ngOnInit() {
this.vehicles$ = this.vehicleService.getVehicles();
if (this.fuelId > 0) {
  this.editClicked();
  this.fuelService.getFuel(this.fuelId).subscribe(data => (
      ......
      this.vehicleList.patchValue(data.vehicle),
      console.log(this.vehicleList.value),
      ......
    ));
}

这是日志输出:

fuel-add-edit.component.ts:55 {id: 20, plate: "random1", fuels: Array(0), journeys: Array(0), maintenances: Array(0)}

这是我选择的垫子

<mat-form-field class="float-left w-75">
  <mat-label>Scegli Automezzo</mat-label>
  <mat-select [formControl]="vehicleList" [errorStateMatcher]="matcher">
    <mat-option *ngFor="let vehicle of vehicles$ | async" [value]="vehicle">
      {{ vehicle.plate }}
    </mat-option>
  </mat-select>
</mat-form-field>

我想在编辑项目时选择“ Scegli automezzo”而不是“ Scegli automezzo”

1 个答案:

答案 0 :(得分:0)

如果值是一个对象,则必须传递[compareWith]函数。有关问题的完整说明,请参见Angular Material Select - Default Value - Reactive Forms中的答案。