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', () => {
695
695
expect ( listOptions . every ( option => ! option . selected ) )
696
696
. toBe ( true , 'Expected every list option to be unselected.' ) ;
697
697
} ) ;
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
+ } ) ;
698
714
} ) ;
699
715
} ) ;
700
716
Original file line number Diff line number Diff line change @@ -290,6 +290,9 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
290
290
/** View to model callback that should be called whenever the selected options change. */
291
291
private _onChange : ( value : any ) => void = ( _ : any ) => { } ;
292
292
293
+ /** Used for storing the values that were assigned before the options were initialized. */
294
+ private _tempValues : string [ ] | null ;
295
+
293
296
/** View to model callback that should be called if the list or its options lost focus. */
294
297
onTouched : ( ) => void = ( ) => { } ;
295
298
@@ -301,6 +304,11 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
301
304
302
305
ngAfterContentInit ( ) : void {
303
306
this . _keyManager = new FocusKeyManager < MatListOption > ( this . options ) . withWrap ( ) ;
307
+
308
+ if ( this . _tempValues ) {
309
+ this . _setOptionsFromValues ( this . _tempValues ) ;
310
+ this . _tempValues = null ;
311
+ }
304
312
}
305
313
306
314
/** Focus the selection-list. */
@@ -369,6 +377,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
369
377
writeValue ( values : string [ ] ) : void {
370
378
if ( this . options ) {
371
379
this . _setOptionsFromValues ( values || [ ] ) ;
380
+ } else {
381
+ this . _tempValues = values ;
372
382
}
373
383
}
374
384
You can’t perform that action at this time.
0 commit comments