angular - 从 Angular 6 mat-selection-list 获取选定值的列表

标签 angular angular-material angular-material-6

如何获取从 Angular Material 中选择的所有值的列表 mat selection list在组件中。给出的示例显示了要显示在模板中而不是组件中的值。我正在尝试修改 this question 中给出的解决方案但它不适合我。这是我当前的代码:

模板:

<mat-selection-list #selected [(ngModel)]="readingTypesSelected" (ngModelChange)="onSelection($event)" >
  <mat-list-option *ngFor="let readingType of readingTypes">
    {{readingType.name}}
  </mat-list-option>
</mat-selection-list>

组件:

    onSelection(e, v) {

    console.log(e);
    console.log(v);    
  }

以下内容被记录到控制台:

enter image description here

我如何从中提取所选选项的实际值?

解决方案:

模板代码的前两行应该是(如已接受解决方案中的 stackblitz 链接中给出的):

<mat-selection-list #selected (selectionChange)="onSelection($event, selected.selectedOptions.selected)" >
      <mat-list-option *ngFor="let readingType of readingTypes" [value] ="readingType">

最佳答案

试试这个

<mat-selection-list #list [(ngModel)]="selectedOptions" (ngModelChange)="onNgModelChange($event)">
    <mat-list-option *ngFor="let shoe of typesOfShoes" [value]="shoe">
      {{shoe}}
    </mat-list-option>
</mat-selection-list>

绑定(bind) [(ngModel)]="selectedOptions" 后,您可以在组件中使用 selectedOptions 变量,它将包含所有选定的项目。

示例:https://stackblitz.com/edit/angular-hdmfwi

关于angular - 从 Angular 6 mat-selection-list 获取选定值的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288318/

相关文章:

css - 在 Angular-material 中将 more_vert 按钮对齐右侧

angular - 类通过过滤扩展 MatTableDataSource 和表

dialog - Angular6 Material Dialog 在按回车键时阻止关闭

angular - 带有延迟操作符的单元测试 Observable

javascript - 如何在 Angular 6 中使用 jquery-ui

Angular mat-selection-list,如何使单个复选框选择类似于单选按钮?

angular-material - 如何编辑角度 Material 中 .mat-select-panel 的默认 css?

node.js - 如何在 MEAN 堆栈应用程序中从后端到前端进行通信?

javascript - ngrx - 一旦分派(dispatch)的操作成功更新商店,如何执行操作?

Angular 6 Material form重置使验证错误出现