File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -695,6 +695,22 @@ describe('MatSelectionList with forms', () => {
695695 expect ( listOptions . every ( option => ! option . selected ) )
696696 . toBe ( true , 'Expected every list option to be unselected.' ) ;
697697 } ) ;
698+
699+ it ( 'should mark options as selected when the value is set before they are initialized' , ( ) => {
700+ fixture . destroy ( ) ;
701+ fixture = TestBed . createComponent ( SelectionListWithFormControl ) ;
702+ selectionListDebug = fixture . debugElement . query ( By . directive ( MatSelectionList ) ) ;
703+ selectionList = selectionListDebug . componentInstance ;
704+
705+ fixture . componentInstance . formControl . setValue ( [ 'opt2' , 'opt3' ] ) ;
706+ fixture . detectChanges ( ) ;
707+
708+ listOptions = fixture . debugElement . queryAll ( By . directive ( MatListOption ) )
709+ . map ( optionDebugEl => optionDebugEl . componentInstance ) ;
710+
711+ expect ( listOptions [ 1 ] . selected ) . toBe ( true , 'Expected second option to be selected.' ) ;
712+ expect ( listOptions [ 2 ] . selected ) . toBe ( true , 'Expected third option to be selected.' ) ;
713+ } ) ;
698714 } ) ;
699715} ) ;
700716
Original file line number Diff line number Diff line change @@ -290,6 +290,9 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
290290 /** View to model callback that should be called whenever the selected options change. */
291291 private _onChange : ( value : any ) => void = ( _ : any ) => { } ;
292292
293+ /** Used for storing the values that were assigned before the options were initialized. */
294+ private _tempValues : string [ ] | null ;
295+
293296 /** View to model callback that should be called if the list or its options lost focus. */
294297 onTouched : ( ) => void = ( ) => { } ;
295298
@@ -301,6 +304,11 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
301304
302305 ngAfterContentInit ( ) : void {
303306 this . _keyManager = new FocusKeyManager < MatListOption > ( this . options ) . withWrap ( ) ;
307+
308+ if ( this . _tempValues ) {
309+ this . _setOptionsFromValues ( this . _tempValues ) ;
310+ this . _tempValues = null ;
311+ }
304312 }
305313
306314 /** Focus the selection-list. */
@@ -369,6 +377,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
369377 writeValue ( values : string [ ] ) : void {
370378 if ( this . options ) {
371379 this . _setOptionsFromValues ( values || [ ] ) ;
380+ } else {
381+ this . _tempValues = values ;
372382 }
373383 }
374384
You can’t perform that action at this time.
0 commit comments