Skip to content

Commit

Permalink
fix(autocomplete): error when closing from a destroyed view
Browse files Browse the repository at this point in the history
Fixes an error that was being thrown, because the autocomplete tries to run change detection on a destroyed view.

Fixes #7315.
  • Loading branch information
crisbeto committed Jan 10, 2018
1 parent af44b9d commit 2cee7d7
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
private _overlayRef: OverlayRef | null;
private _portal: TemplatePortal<any>;
private _panelOpen: boolean = false;
private _componentDestroyed = false;

/** Strategy that is used to position the panel. */
private _positionStrategy: ConnectedPositionStrategy;
Expand Down Expand Up @@ -150,6 +151,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
@Optional() @Inject(DOCUMENT) private _document: any) {}

ngOnDestroy() {
this._componentDestroyed = true;
this._destroyPanel();
this._escapeEventStream.complete();
}
Expand Down Expand Up @@ -177,11 +179,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();
}
}
}

Expand Down

0 comments on commit 2cee7d7

Please sign in to comment.