Mat-select:让它选择第一个mat-option

时间:2018-11-23 17:48:51

标签: angular drop-down-menu angular-material

我有很多角度材料选择下拉菜单,并且根据其他值以反应性的方式更新了它们各自的垫选项(换句话说,对选项进行了过滤)。看起来像那样

<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 个答案:

答案 0 :(得分:1)

只要更新选择列表,就只需要设置选择的值即可。例如:

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);
  }
}
相关问题