在角度6的下拉列表中设置默认值

时间:2019-02-05 10:20:15

标签: angular angular-material

我有一个名为 list 的组件,其中,我在下拉列表中显示了customers中的所有api,如下图所示:

enter image description here

如何将第一位客户设置为默认客户?像这样:

enter image description here

DEMO

1 个答案:

答案 0 :(得分:2)

只需添加:

selectedCustomer = '01';

在模板中使用[(ngModel)]

<div class="main-div">
  <h3>List</h3>
  <mat-form-field>
    <mat-select 
      placeholder="Select Customer" 
      [(ngModel)]="selectedCustomer">
      <mat-option 
        *ngFor="let customer of customers" 
        [value]="customer.id" 
        (click)="selected($event, customer.id)">
        {{customer.name}}
      </mat-option>
    </mat-select>
  </mat-form-field>
</div>

  

这是您推荐的Working Sample StackBlitz

相关问题