uhetz
uhetz

Reputation: 412

Angular. Value from Event on MatSelectionListChange is undefined

I'm trying to get a value of a changed element in a list, but I'm getting always value undefined while the property checked is right.

HTML

<div>
    <mat-selection-list #costUnits
    [(ngModel)]="selectedCostUnits"
    (selectionChange)="onSelectionChange($event)"
    >
        <mat-list-option *ngFor="let label of labels" selected="true">
            {{ label }}
        </mat-list-option>
    </mat-selection-list>
</div>

TS

  onSelectionChange(event: MatSelectionListChange) {
    console.log(event)
  }

For example: I've clicked twice on one element in the list that was checked and got the following outputs.

console.log output

Upvotes: 4

Views: 3467

Answers (1)

uhetz
uhetz

Reputation: 412

I got it. The value wasn't bound. The right code of the component is it:

<mat-list-option 
  *ngFor="let label of labels" 
  selected="true" 
  [value]="label"
  >
    {{ label }}
</mat-list-option>

Upvotes: 3

Related Questions