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 angular#7315.
  • Loading branch information
crisbeto committed Sep 27, 2017
1 parent 3571f68 commit e3e77bc
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class MdAutocompleteTrigger 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 @@ -156,6 +157,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
@Optional() @Inject(DOCUMENT) private _document: any) {}

ngOnDestroy() {
this._componentDestroyed = true;
this._destroyPanel();
}

Expand All @@ -179,7 +181,9 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {

this._resetPlaceholder();

if (this._panelOpen) {
// 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._panelOpen && !this._componentDestroyed) {
this._panelOpen = false;

// We need to trigger change detection manually, because
Expand Down

0 comments on commit e3e77bc

Please sign in to comment.