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 @@ -679,6 +679,22 @@ describe('MatSelectionList with forms', () => {
679679 expect ( listOptions . every ( option => ! option . selected ) )
680680 . toBe ( true , 'Expected every list option to be unselected.' ) ;
681681 } ) ;
682+
683+ it ( 'should mark options as selected when the value is set before they are initialized' , ( ) => {
684+ fixture . destroy ( ) ;
685+ fixture = TestBed . createComponent ( SelectionListWithFormControl ) ;
686+ selectionListDebug = fixture . debugElement . query ( By . directive ( MatSelectionList ) ) ;
687+ selectionList = selectionListDebug . componentInstance ;
688+
689+ fixture . componentInstance . formControl . setValue ( [ 'opt2' , 'opt3' ] ) ;
690+ fixture . detectChanges ( ) ;
691+
692+ listOptions = fixture . debugElement . queryAll ( By . directive ( MatListOption ) )
693+ . map ( optionDebugEl => optionDebugEl . componentInstance ) ;
694+
695+ expect ( listOptions [ 1 ] . selected ) . toBe ( true , 'Expected second option to be selected.' ) ;
696+ expect ( listOptions [ 2 ] . selected ) . toBe ( true , 'Expected third option to be selected.' ) ;
697+ } ) ;
682698 } ) ;
683699} ) ;
684700
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. */
@@ -368,6 +376,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
368376 writeValue ( values : string [ ] ) : void {
369377 if ( this . options ) {
370378 this . _setOptionsFromValues ( values || [ ] ) ;
379+ } else {
380+ this . _tempValues = values ;
371381 }
372382 }
373383
You can’t perform that action at this time.
0 commit comments