Components: mat-selection-list 2-way data binding

Created on 18 Apr 2018  路  7Comments  路  Source: angular/components

Bug, feature request, or proposal:

I'm using a mat-selection-list with mat-list-option as per your docs. From a StackOverflow answer, it seems that ngModel is usable.

What is the expected behavior?

Two way data-binding between a list of selections and what is actually selected (rendered).

What is the current behavior?

ngModel doesn't consistently reflect what is selected, and modifying what is selected in the *.component.ts has no effect on the [rendered] view.

What are the steps to reproduce?

https://stackblitz.com/edit/angular-3fffzf

What is the use-case or motivation for changing an existing behavior?

To enable various use cases.

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

Tested with @angular/material version 5.2.5

Is there anything else we should know?

Been working on angular-vertical-tabs; a basic vertical tabs component that uses mat-selection-list and @angular/flex-layout.

Most helpful comment

I don't know if it helps anyone, but it didn't work for me because I forgot to bind a [value] to the mat-list-options. Once I added the [value] it worked...

All 7 comments

@SamuelMarks For me it's working as intended.
You need to set value to list options to make this work: https://stackblitz.com/edit/angular-3fffzf-p6naz3?file=app/list-selection-example.ts

it does not work in v6

I'm using "@angular/material": "^6.2.1" and it only works 1-way: model changes when I interact with the list, but the list is not changed when I change model's values

@tinayuangao It only works with a string list as a model. If you try to do your example on stacklitz with a custom class, it wouldn't work. What I mean is it doesn't reflect changes if you change selected items from code.

The comments upthread are kind of unclear. Is mat-selection-list supposed to support 2-way binding of any kind? If so, how? If not, can this ticket be changed to "please support 2-way binding", or do I need to open a new ticket for that request?

ETA: never mind. The above Stackblitz was not a good example (it read from a template var instead of a bound controller class member), but 2 way binding does already work. I forked the example to show what's actually going on, and yes, you can 2-way bind a string array with the values of the selected items.

I don't know if it helps anyone, but it didn't work for me because I forgot to bind a [value] to the mat-list-options. Once I added the [value] it worked...

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mmalerba picture mmalerba  路  127Comments

jelbourn picture jelbourn  路  171Comments

jeffbcross picture jeffbcross  路  126Comments

julianobrasil picture julianobrasil  路  78Comments

AlanCrevon picture AlanCrevon  路  107Comments