mat-select multiple设置一个数组Angular4中的选中选项

前端之家收集整理的这篇文章主要介绍了mat-select multiple设置一个数组Angular4中的选中选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Angular 4.

我需要帮助来设置mat-select的选项,所以这里是问题所在:

1.首先我得到两个变量:options和checkedOptions

options: string[];
checkedOptions: string[] //This comes from the BD;

2.So选项是所有选项,checkedOptions来自BD,如下所示:

options = ["o1","o2","o3","o4",... "oN"]
checkedOptions = ["o2","o4"]

3.我打印这样的选项:

<mat-form-field floatPlaceholder="always" color="accent" class="input-all">
  <mat-select multiple placeholder="{{ par.label }}">
    <mat-option *ngFor="let op of options" [value]="op">{{ op }}</mat-option>
  </mat-select>
</mat-form-field>

>所以在mat-select中是所有选项……但我想要的是,在选项列表中,只需检查哪些是在checkedOptions列表中.我怎么做?

请帮忙.

解决方法

在这种情况下,您必须在mat-select-tag中使用ngModel并将其绑定到您的选择列表.这是解决方案:

HTML

<mat-form-field floatPlaceholder="always" color="accent" class="input-all">

   <mat-select [(ngModel)]="checkedOptions" multiple placeholder="{{ par.label }}">

       <mat-option *ngFor="let op of options" [value]="op">{{ op }}</mat-option>

   </mat-select>

</mat-form-field>

猜你在找的Angularjs相关文章