@@ -180,14 +180,8 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
180180 }
181181
182182 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 ;
191185 }
192186
193187 /**
@@ -261,6 +255,19 @@ export class MatButtonToggleGroup extends _MatButtonToggleGroupMixinBase impleme
261255 return this . _selectionModel . isSelected ( toggle ) ;
262256 }
263257
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+
264271 /** Updates the selection state of the toggles in the group based on a value. */
265272 private _setSelectionByValue ( value : any | any [ ] ) {
266273 // If the toggles haven't been initialized yet, save the value for later.
@@ -409,6 +416,10 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
409416 this . name = this . buttonToggleGroup . name ;
410417 }
411418
419+ if ( this . buttonToggleGroup && this . buttonToggleGroup . _isPrechecked ( this ) ) {
420+ this . checked = true ;
421+ }
422+
412423 this . _focusMonitor . monitor ( this . _elementRef . nativeElement , true ) ;
413424 }
414425
@@ -449,8 +460,8 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
449460 * update bound properties of the radio button.
450461 */
451462 _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.
454465 this . _changeDetectorRef . markForCheck ( ) ;
455466 }
456467}
0 commit comments