@@ -180,14 +180,8 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
180
180
}
181
181
182
182
ngAfterContentInit ( ) {
183
- // If there was an attempt to assign a value before init, use it to set the
184
- // initial selection, otherwise check the `checked` state of the toggles.
185
- if ( typeof this . _tempValue !== 'undefined' ) {
186
- this . _setSelectionByValue ( this . _tempValue ) ;
187
- this . _tempValue = undefined ;
188
- } else {
189
- this . _selectionModel . select ( ...this . _buttonToggles . filter ( toggle => toggle . checked ) ) ;
190
- }
183
+ this . _selectionModel . select ( ...this . _buttonToggles . filter ( toggle => toggle . checked ) ) ;
184
+ this . _tempValue = undefined ;
191
185
}
192
186
193
187
/**
@@ -261,6 +255,19 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
261
255
return this . _selectionModel . isSelected ( toggle ) ;
262
256
}
263
257
258
+ /** Determines whether a button toggle should be checked on init. */
259
+ _isPrechecked ( toggle : MatButtonToggle ) {
260
+ if ( typeof this . _tempValue === 'undefined' ) {
261
+ return false ;
262
+ }
263
+
264
+ if ( this . multiple && Array . isArray ( this . _tempValue ) ) {
265
+ return ! ! this . _tempValue . find ( value => toggle . value != null && value === toggle . value ) ;
266
+ }
267
+
268
+ return toggle . value === this . _tempValue ;
269
+ }
270
+
264
271
/** Updates the selection state of the toggles in the group based on a value. */
265
272
private _setSelectionByValue ( value : any | any [ ] ) {
266
273
// If the toggles haven't been initialized yet, save the value for later.
@@ -409,6 +416,10 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
409
416
this . name = this . buttonToggleGroup . name ;
410
417
}
411
418
419
+ if ( this . buttonToggleGroup && this . buttonToggleGroup . _isPrechecked ( this ) ) {
420
+ this . checked = true ;
421
+ }
422
+
412
423
this . _focusMonitor . monitor ( this . _elementRef . nativeElement , true ) ;
413
424
}
414
425
@@ -449,8 +460,8 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
449
460
* update bound properties of the radio button.
450
461
*/
451
462
_markForCheck ( ) {
452
- // When group value changes, the button will not be notified. Use `markForCheck` to explicit
453
- // update button toggle's status
463
+ // When the group value changes, the button will not be notified.
464
+ // Use `markForCheck` to explicit update button toggle's status.
454
465
this . _changeDetectorRef . markForCheck ( ) ;
455
466
}
456
467
}
0 commit comments