Skip to content

Commit e3e77bc

Browse files
committed
fix(autocomplete): error when closing from a destroyed view
Fixes an error that was being thrown, because the autocomplete tries to run change detection on a destroyed view. Fixes #7315.
1 parent 3571f68 commit e3e77bc

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
117117
private _overlayRef: OverlayRef | null;
118118
private _portal: TemplatePortal<any>;
119119
private _panelOpen: boolean = false;
120+
private _componentDestroyed = false;
120121

121122
/** Strategy that is used to position the panel. */
122123
private _positionStrategy: ConnectedPositionStrategy;
@@ -156,6 +157,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
156157
@Optional() @Inject(DOCUMENT) private _document: any) {}
157158

158159
ngOnDestroy() {
160+
this._componentDestroyed = true;
159161
this._destroyPanel();
160162
}
161163

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

180182
this._resetPlaceholder();
181183

182-
if (this._panelOpen) {
184+
// Note that in some cases this can end up being called after the component is destroyed.
185+
// Add a check to ensure that we don't try to run change detection on a destroyed view.
186+
if (this._panelOpen && !this._componentDestroyed) {
183187
this._panelOpen = false;
184188

185189
// We need to trigger change detection manually, because

0 commit comments

Comments
 (0)