当您需要mat select的默认值时,您需要在启动reactive form例如时指定它。
states = [
{name: 'Arizona', abbrev: 'AZ'},
{name: 'California', abbrev: 'CA'},
{name: 'Colorado', abbrev: 'CO'},
{name: 'New York', abbrev: 'NY'},
{name: 'Pennsylvania', abbrev: 'PA'},
];
form = new FormGroup({
state: new FormControl(this.states[3].abbrev),
});
<mat-select formControlName="state">
<mat-option *ngFor="let state of states" [value]="state.abbrev">
{{state.name}}
</mat-option>
</mat-select>
现在,当您的*ngfor值与formcontrol中提供的值匹配时。
这也是显示从api检索到的数据的方式,以便用收集到的数据自动填充字段。
希望这能有所帮助。