简体   繁体   English

Angular 材料多 select 获取隐藏的选定值

[英]Angular material multi select get selected value on hide

Scenario:设想:

  • I have a filter in angular material multi-select我在 angular 材质多选中有一个过滤器

Todo:去做:

  • I want to get the selected values on hide of dropdown..我想在隐藏的下拉列表中获取选定的值..

    I googled and tried to find the doc but could not found any ref to add hide event to miltiselect我用谷歌搜索并试图找到文档,但找不到任何参考来将隐藏事件添加到 miltiselect

Demo where multi-select values are available after closed mat-select dropdown 在关闭mat-select下拉列表后可以使用多选值的演示

Application Code : https://stackblitz.com/edit/angular-values-access-after-dropdown-close?file=src/app/app.component.html 应用代码: https //stackblitz.com/edit/angular-values-access-after-dropdown-close?file = src / app / app.component.html

Approach : 方法:

  • Keep a variable to store multi-select values. 保留变量以存储multi-select值。
    Use openedChange event as (openedChange)="comboChange($event)" , and in that store the selected values into an array or a different variable. 使用openedChange事件作为(openedChange)="comboChange($event)" ,并在该存储中将选定的值存储到数组或不同的变量中。
    Event value is false if dropdown is closed, so i have used it in the demo code. 如果下拉列表已关闭,则事件值为false ,因此我在演示代码中使用了它。
  • Also, mat-select is used with [formControl]="toppings" , so in any other method, you can access its value using this.toppings.value , it will return an array with selected values. 此外, mat-select[formControl]="toppings" ,因此在任何其他方法中,您可以使用this.toppings.value访问其值,它将返回一个包含所选值的数组。

Adding Formcontrol variable like below we can enumerate the selected items from a angular multiselect dropdown添加如下 Formcontrol 变量,我们可以从 angular 多选下拉列表中枚举所选项目

 <mat-select multiple placeholder="Reason" [formControl]="selectedReasons">
        <mat-option value="option1">option1</mat-option>
        <mat-option value="option2">option2</mat-option>
        <mat-option value="option3">option3</mat-option>
        <mat-option value="option4">option4</mat-option>
      </mat-select>

TS TS

 selectedReasons = new FormControl();

  reasonDropdownChangeEvent(event: any) {
console.log(this.selectedReasons.value.toString());

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM