代码之家  ›  专栏  ›  技术社区  ›  Learning is a mess

垫选择:让它选择第一个垫选项

  •  0
  • Learning is a mess  · 技术社区  · 7 年前

    我有许多“角度材质选择”下拉列表,它们各自的材质选项会根据其他值以反应方式更新(换句话说,会对选项进行过滤)。它可以看起来像那样

    <mat-select #selects (selectionChange)='formChanges()' [placeholder]='element.label' [disabled]='false' required>
      <ng-container *ngFor="let opt of item.options; index as index">
      <mat-option *ngIf="!videoService.filterStore[item.id] || videoService.filterStore[item.id].filter.includes(index)" [value]="opt">
        {{opt.label}}
      </mat-option>
    </ng-container>
    </mat-select>
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   G. Tranter    7 年前

    只要在列表更新时设置select的值即可。例如:

    export class SelectExample {
    
      @ViewChild(MatSelect) select: MatSelect;
    
      updateSelectOptions() {
        // update the options
        ...
    
        // update the select value to the first item
        // might need to use a timeout to wait until the select has reloaded the options
        setTimeout(() => this.select.value = this.select.options[0].value);
      }
    }