Angular material 更改mat选择默认值
即使我以正确的方式设置了value属性,也无法更改默认值或我的mat select项Angular material 更改mat选择默认值,angular-material,Angular Material,即使我以正确的方式设置了value属性,也无法更改默认值或我的mat select项 ngOnInit() { this.vehicles$ = this.vehicleService.getVehicles(); if (this.fuelId > 0) { this.editClicked(); this.fuelService.getFuel(this.fuelId).subscribe(data => ( ...... this.vehicle
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>
斯凯格利自动中音
{{vehicle.plate}
我希望在编辑项目时已选择正确的车辆,而不是“Scegli automezzo”如果值是对象,则必须传入
[compareWith]
函数。有关问题的完整解释,请参见中的答案。我相信这是的副本:如果值是对象,则需要使用[compareWith]。您是正确的!从来没有这样想过这个问题!谢谢。如果你照我说的做,我就接受