Skip to content

MatSelectionList: selectedOptions.selected not up to date in onChange #8584

@probert94

Description

@probert94

Bug, feature request, or proposal:

When I listen to change events of the MatSelectionList and I use selectedOptions.selected to know the current selected values, they are not always up to date.
Problem lies in Selection.ts#125:
The onChange.next is called, before the cached value of _selected is reseted.
Therefore the get selected() will return the old value.
The bug only seems to occure, if you first set a value of another material-component, so it might be a timing-problem.

What is the expected behavior?

The cached value of _selected should be reset, so that I get the current value in onChange.

What is the current behavior?

When calling selectedOptions.selected, I sometimes get the previous value.

What are the steps to reproduce?

  1. Create a Angular-Material application with a MatSelectionList. You might need another component (for example an input) too, to reproduce the bug.
  2. Add an onChange-Listener to the MatSelectionList and console.log the value of selectedOptions.selected.
  3. Focus the input (or any other material component in your app).
  4. Select one of the MatListOptions inside the MatSelectionList and look at the value in the console.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

I am using @angular/material and @angular/cdk version 5.0.0-rc0 as well as @angular version 5.0.1

Is there anything else we should know?

A simple window.setTimeout(() => console.log(selectedOptions.selected)) seems to solve the problem.

Metadata

Metadata

Assignees

Labels

P4A relatively minor issue that is not relevant to core functions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions