Skip to content

Commit

Permalink
fix(material/radio): avoid error if destroyed quickly (#29507)
Browse files Browse the repository at this point in the history
Fixes an error that can happen if a radio button is destroyed before `ngAfterViewInit` has run.
  • Loading branch information
crisbeto committed Jul 29, 2024
1 parent 4292e1b commit fd47a0e
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/material/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ export class MatRadioButton implements OnInit, AfterViewInit, DoCheck, OnDestroy
}

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

Expand Down

0 comments on commit fd47a0e

Please sign in to comment.