@@ -221,10 +221,10 @@ export class MatListOption extends _MatListOptionMixinBase
221
221
return this . _element . nativeElement ;
222
222
}
223
223
224
- /** Sets the selected state of the option. */
225
- _setSelected ( selected : boolean ) {
224
+ /** Sets the selected state of the option. Returns whether the value has changed. */
225
+ _setSelected ( selected : boolean ) : boolean {
226
226
if ( selected === this . _selected ) {
227
- return ;
227
+ return false ;
228
228
}
229
229
230
230
this . _selected = selected ;
@@ -236,6 +236,7 @@ export class MatListOption extends _MatListOptionMixinBase
236
236
}
237
237
238
238
this . _changeDetector . markForCheck ( ) ;
239
+ return true ;
239
240
}
240
241
}
241
242
@@ -302,7 +303,6 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
302
303
303
304
constructor ( private _element : ElementRef , @Attribute ( 'tabindex' ) tabIndex : string ) {
304
305
super ( ) ;
305
-
306
306
this . tabIndex = parseInt ( tabIndex ) || 0 ;
307
307
}
308
308
@@ -346,14 +346,12 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
346
346
347
347
/** Selects all of the options. */
348
348
selectAll ( ) {
349
- this . options . forEach ( option => option . _setSelected ( true ) ) ;
350
- this . _reportValueChange ( ) ;
349
+ this . _setAllOptionsSelected ( true ) ;
351
350
}
352
351
353
352
/** Deselects all of the options. */
354
353
deselectAll ( ) {
355
- this . options . forEach ( option => option . _setSelected ( false ) ) ;
356
- this . _reportValueChange ( ) ;
354
+ this . _setAllOptionsSelected ( false ) ;
357
355
}
358
356
359
357
/** Sets the focused option of the selection-list. */
@@ -479,6 +477,26 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
479
477
}
480
478
}
481
479
480
+ /**
481
+ * Sets the selected state on all of the options
482
+ * and emits an event if anything changed.
483
+ */
484
+ private _setAllOptionsSelected ( isSelected : boolean ) {
485
+ // Keep track of whether anything changed, because we only want to
486
+ // emit the changed event when something actually changed.
487
+ let hasChanged = false ;
488
+
489
+ this . options . forEach ( option => {
490
+ if ( option . _setSelected ( isSelected ) ) {
491
+ hasChanged = true ;
492
+ }
493
+ } ) ;
494
+
495
+ if ( hasChanged ) {
496
+ this . _reportValueChange ( ) ;
497
+ }
498
+ }
499
+
482
500
/**
483
501
* Utility to ensure all indexes are valid.
484
502
* @param index The index to be checked.
0 commit comments