更新时间:2023-02-02 12:15:31
为模板中的值使用绑定.
Use a binding for the value in your template.
value="{{ option.id }}"
应该是
[value]="option.id"
然后在您选择的值中使用ngModel
而不是value
.
And in your selected value use ngModel
instead of value
.
<mat-select [(value)]="selected2">
应该是
<mat-select [(ngModel)]="selected2">
完整代码:
<div>
<mat-select [(ngModel)]="selected2">
<mat-option *ngFor="let option of options2" [value]="option.id">{{ option.name }}</mat-option>
</mat-select>
</div>
从版本2.0.0-beta.12起 a> 材料选择现在接受mat-form-field
元素作为父元素,因此与其他物料输入控件一致.升级后,将div
元素替换为mat-form-field
元素.
On a side note as of version 2.0.0-beta.12 the material select now accepts a mat-form-field
element as the parent element so it is consistent with the other material input controls. Replace the div
element with mat-form-field
element after you upgrade.
<mat-form-field>
<mat-select [(ngModel)]="selected2">
<mat-option *ngFor="let option of options2" [value]="option.id">{{ option.name }}</mat-option>
</mat-select>
</mat-form-field>