角度材质为选择设置默认值

时间:2021-01-19 15:26:43

标签: angular angular-material

我无法在我的页面上为 select 设置默认值。我从数据库中收集值,目前我只有一个值,这就是我想将其设置为默认值的原因。

这是我的代码:

<mat-form-field appearance="fill">
  <mat-select (selectionChange)="contractSelect($event.value)">
    <mat-option *ngFor="let contractType of contracTypes" [value]="contractType">
      {{contractType.description}}
    </mat-option>
  </mat-select>
</mat-form-field>

1 个答案:

答案 0 :(得分:0)

假设 contractType 存在,您可以在 mat-select 处添加双向绑定,如下面的代码:

<mat-form-field appearance="fill">
  <mat-select (selectionChange)="contractSelect($event.value)" [(ngModel)]="{{CONTRACT_TYPE_VARIABLE_ON_TS}}">
    <mat-option *ngFor="let contractType of contracTypes" [value]="contractType">
      {{contractType.description}}
    </mat-option>
  </mat-select>
</mat-form-field>