@@ -41,6 +41,7 @@ import {
41
41
mixinTabIndex ,
42
42
} from '@angular/material/core' ;
43
43
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
44
+ import { Subscription } from 'rxjs/Subscription' ;
44
45
45
46
46
47
/** @docs -private */
@@ -112,8 +113,8 @@ export class MatListOption extends _MatListOptionMixinBase
112
113
implements AfterContentInit , OnDestroy , OnInit , FocusableOption , CanDisableRipple {
113
114
114
115
private _lineSetter : MatLineSetter ;
115
- private _selected : boolean = false ;
116
- private _disabled : boolean = false ;
116
+ private _selected = false ;
117
+ private _disabled = false ;
117
118
118
119
/** Whether the option has focus. */
119
120
_hasFocus : boolean = false ;
@@ -292,7 +293,7 @@ export class MatListOption extends _MatListOptionMixinBase
292
293
changeDetection : ChangeDetectionStrategy . OnPush
293
294
} )
294
295
export class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption ,
295
- CanDisable , CanDisableRipple , HasTabIndex , AfterContentInit , ControlValueAccessor {
296
+ CanDisable , CanDisableRipple , HasTabIndex , AfterContentInit , ControlValueAccessor , OnDestroy {
296
297
297
298
/** The FocusKeyManager which handles focus. */
298
299
_keyManager : FocusKeyManager < MatListOption > ;
@@ -313,6 +314,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
313
314
/** Used for storing the values that were assigned before the options were initialized. */
314
315
private _tempValues : string [ ] | null ;
315
316
317
+ private _modelChanges = Subscription . EMPTY ;
318
+
316
319
/** View to model callback that should be called if the list or its options lost focus. */
317
320
_onTouched : ( ) => void = ( ) => { } ;
318
321
@@ -329,6 +332,25 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
329
332
this . _setOptionsFromValues ( this . _tempValues ) ;
330
333
this . _tempValues = null ;
331
334
}
335
+
336
+ // Sync external changes to the model back to the options.
337
+ this . _modelChanges = this . selectedOptions . onChange ! . subscribe ( event => {
338
+ if ( event . added ) {
339
+ for ( let item of event . added ) {
340
+ item . selected = true ;
341
+ }
342
+ }
343
+
344
+ if ( event . removed ) {
345
+ for ( let item of event . removed ) {
346
+ item . selected = false ;
347
+ }
348
+ }
349
+ } ) ;
350
+ }
351
+
352
+ ngOnDestroy ( ) {
353
+ this . _modelChanges . unsubscribe ( ) ;
332
354
}
333
355
334
356
/** Focus the selection-list. */
0 commit comments