I am trying to access the selected options of a material mat-selection-list component (version 7). I have found documentation for selectedOptions here.
I have fiddeld together some lines of html/ts that seem to give me access to the desired data. However I do not think it is the right way. However I could not find advice using "famous search engine".
I have folloing 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>
And corresponding typescript file (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;
};
}
Question would be: How do I do it correctly?
PS: I am using @angular/core 7.0.0 and material 7.0.1