角2/4/5材质:如何获得多选下拉菜单的未经检查的值

时间:2018-08-22 04:34:31

标签: angular typescript angular5

所有我正在使用角材进行多选下拉菜单,我能够得到
选定的值,任何一项都可以帮助获取多选下拉菜单的未经检查的值

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式进行检查,

component.html

<mat-form-field>
    <mat-select placeholder="Toppings" (selectionChange)="onChange($event.value)" multiple>
        <mat-option *ngFor="let topping of toppingList" [value]="topping">{{topping}}</mat-option>
    </mat-select>
</mat-form-field>

component.ts

toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'];
selectedList: any = []; // store selected options here

onChange(event) {

   let missing = null;

   for (let i = 0; i < this.selectedList.length; i++) {
      if (event.indexOf(this.selectedList[i]) == -1) missing = this.selectedList[i];      // Current[i] isn't in prev
   }

    if (missing)
      alert(missing);

    this.selectedList = event;

  }

这是Stackblitz演示