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', () => {
679
679
expect ( listOptions . every ( option => ! option . selected ) )
680
680
. toBe ( true , 'Expected every list option to be unselected.' ) ;
681
681
} ) ;
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
+ } ) ;
682
698
} ) ;
683
699
} ) ;
684
700
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. */
@@ -368,6 +376,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
368
376
writeValue ( values : string [ ] ) : void {
369
377
if ( this . options ) {
370
378
this . _setOptionsFromValues ( values || [ ] ) ;
379
+ } else {
380
+ this . _tempValues = values ;
371
381
}
372
382
}
373
383
You can’t perform that action at this time.
0 commit comments