且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角材料:垫选择不选择默认

更新时间: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>