Skip to content

Commit fd47a0e

Browse files
authoredJul 29, 2024··
fix(material/radio): avoid error if destroyed quickly (#29507)
Fixes an error that can happen if a radio button is destroyed before `ngAfterViewInit` has run.
1 parent 4292e1b commit fd47a0e

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed
 

‎src/material/radio/radio.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ export class MatRadioButton implements OnInit, AfterViewInit, DoCheck, OnDestroy
671671
}
672672

673673
ngOnDestroy() {
674-
this._inputElement.nativeElement.removeEventListener('click', this._onInputClick);
674+
// We need to null check in case the button was destroyed before `ngAfterViewInit`.
675+
this._inputElement?.nativeElement.removeEventListener('click', this._onInputClick);
675676
this._focusMonitor.stopMonitoring(this._elementRef);
676677
this._removeUniqueSelectionListener();
677678
}
@@ -713,7 +714,7 @@ export class MatRadioButton implements OnInit, AfterViewInit, DoCheck, OnDestroy
713714
if (!this.disabled || this.disabledInteractive) {
714715
// Normally the input should be focused already, but if the click
715716
// comes from the touch target, then we might have to focus it ourselves.
716-
this._inputElement.nativeElement.focus();
717+
this._inputElement?.nativeElement.focus();
717718
}
718719
}
719720

0 commit comments

Comments
 (0)
Please sign in to comment.