【发布时间】:2019-01-16 20:13:00
【问题描述】:
我正在尝试访问材质 mat-selection-list 组件(版本 7)的选定选项。我找到了 selectedOptions here 的文档。
我整理了一些似乎可以让我访问所需数据的 html/ts 行。但是,我认为这不是正确的方法。但是我找不到使用“著名搜索引擎”的建议。
我有以下 HTML (pizza.component.html)
<h1>Pizzas</h1>
<mat-selection-list #pizzaList (selectionChange)="onPizzasChange($event)">
<mat-list-option *ngFor="let pizza of pizzas">
{{pizza}}
</mat-list-option>
</mat-selection-list>
以及对应的打字稿文件(pizza.component.ts)
import { MatButtonModule } from '@angular/material/button';
import { Component, OnInit } from '@angular/core';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { Pizza } from '../pizza';
@Component({
selector: 'app-pizza-view',
templateUrl: './pizza-view.component.html',
styleUrls: ['./pizza-view.component.css']
})
export class PizzaViewComponent implements OnInit {
pizzas: any;
constructor( ) { }
ngOnInit() {
pizzas = [ 'Margherita', 'Funghi', 'Quattro Stagioni', 'Calzone' ];
}
onPizzasChange(event) {
console.log("selectedOptions:", event.option.selectionList.selectedOptions._selection;
};
}
问题是:我该如何正确地做到这一点?
PS:我正在使用@angular/core 7.0.0 和材料 7.0.1
【问题讨论】: