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