@@ -118,6 +118,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
118
118
private _overlayRef : OverlayRef | null ;
119
119
private _portal : TemplatePortal < any > ;
120
120
private _panelOpen : boolean = false ;
121
+ private _componentDestroyed = false ;
121
122
122
123
/** Strategy that is used to position the panel. */
123
124
private _positionStrategy : ConnectedPositionStrategy ;
@@ -150,6 +151,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
150
151
@Optional ( ) @Inject ( DOCUMENT ) private _document : any ) { }
151
152
152
153
ngOnDestroy ( ) {
154
+ this . _componentDestroyed = true ;
153
155
this . _destroyPanel ( ) ;
154
156
this . _escapeEventStream . complete ( ) ;
155
157
}
@@ -177,11 +179,15 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
177
179
this . _closingActionsSubscription . unsubscribe ( ) ;
178
180
}
179
181
180
- // We need to trigger change detection manually, because
181
- // `fromEvent` doesn't seem to do it at the proper time.
182
- // This ensures that the label is reset when the
183
- // user clicks outside.
184
- this . _changeDetectorRef . detectChanges ( ) ;
182
+ // Note that in some cases this can end up being called after the component is destroyed.
183
+ // Add a check to ensure that we don't try to run change detection on a destroyed view.
184
+ if ( ! this . _componentDestroyed ) {
185
+ // We need to trigger change detection manually, because
186
+ // `fromEvent` doesn't seem to do it at the proper time.
187
+ // This ensures that the label is reset when the
188
+ // user clicks outside.
189
+ this . _changeDetectorRef . detectChanges ( ) ;
190
+ }
185
191
}
186
192
}
187
193
0 commit comments