mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(autocomplete): error when closing from a destroyed view (#7365)
Fixes an error that was being thrown, because the autocomplete tries to run change detection on a destroyed view. Fixes #7315.
This commit is contained in:
committed by
Jeremy Elbourn
parent
8298db1c9c
commit
2b05106d3e
@@ -119,6 +119,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
|
||||
private _overlayRef: OverlayRef | null;
|
||||
private _portal: TemplatePortal;
|
||||
private _panelOpen: boolean = false;
|
||||
private _componentDestroyed = false;
|
||||
|
||||
/** Strategy that is used to position the panel. */
|
||||
private _positionStrategy: ConnectedPositionStrategy;
|
||||
@@ -151,6 +152,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
|
||||
@Optional() @Inject(DOCUMENT) private _document: any) {}
|
||||
|
||||
ngOnDestroy() {
|
||||
this._componentDestroyed = true;
|
||||
this._destroyPanel();
|
||||
this._closeKeyEventStream.complete();
|
||||
}
|
||||
@@ -178,11 +180,15 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
|
||||
this._closingActionsSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
// We need to trigger change detection manually, because
|
||||
// `fromEvent` doesn't seem to do it at the proper time.
|
||||
// This ensures that the label is reset when the
|
||||
// user clicks outside.
|
||||
this._changeDetectorRef.detectChanges();
|
||||
// Note that in some cases this can end up being called after the component is destroyed.
|
||||
// Add a check to ensure that we don't try to run change detection on a destroyed view.
|
||||
if (!this._componentDestroyed) {
|
||||
// We need to trigger change detection manually, because
|
||||
// `fromEvent` doesn't seem to do it at the proper time.
|
||||
// This ensures that the label is reset when the
|
||||
// user clicks outside.
|
||||
this._changeDetectorRef.detectChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user