【问题标题】:To set the default value in the dropdown in angular 6在 Angular 6 的下拉菜单中设置默认值
【发布时间】:2019-02-05 18:29:26
【问题描述】:

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

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

DEMO

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    只需添加这个:

    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 供您参考。

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 2019-12-28
      • 2018-09-29
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 2021-11-24
      相关资源
      最近更新 更多