【发布时间】:2018-09-12 05:54:45
【问题描述】:
如何获取组件中从 Angular 材质 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);
}
以下内容正在记录到控制台:
如何从中提取所选选项的实际值?
解决方案:
模板代码的前两行应该是(在接受的解决方案中的 stackblitz 链接中给出):
<mat-selection-list #selected (selectionChange)="onSelection($event, selected.selectedOptions.selected)" >
<mat-list-option *ngFor="let readingType of readingTypes" [value] ="readingType">
【问题讨论】:
-
你有什么错误吗?
-
我得到一个以 MatListOption 作为值的数组。不知道如何提取实际值。
-
您提到的解决方案也适用于 Angular 8,Materials 8。谢谢
标签: angular angular-material angular-material-6