@@ -15,7 +15,6 @@ import {
1515 PositionStrategy ,
1616 ScrollStrategy ,
1717} from '@angular/cdk/overlay' ;
18- import { TemplatePortal } from '@angular/cdk/portal' ;
1918import { DOCUMENT } from '@angular/common' ;
2019import { filter , take , switchMap , delay , tap , map } from 'rxjs/operators' ;
2120import {
@@ -30,7 +29,6 @@ import {
3029 NgZone ,
3130 OnDestroy ,
3231 Optional ,
33- ViewContainerRef ,
3432} from '@angular/core' ;
3533import { ViewportRuler } from '@angular/cdk/scrolling' ;
3634import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
@@ -117,9 +115,9 @@ export function getMatAutocompleteMissingPanelError(): Error {
117115} )
118116export class MatAutocompleteTrigger implements ControlValueAccessor , OnDestroy {
119117 private _overlayRef : OverlayRef | null ;
120- private _portal : TemplatePortal ;
121118 private _componentDestroyed = false ;
122119 private _autocompleteDisabled = false ;
120+ private _autocomplete : MatAutocomplete ;
123121 private _scrollStrategy : ( ) => ScrollStrategy ;
124122
125123 /** Old value of the native input. Used to work around issues with the `input` event on IE. */
@@ -132,7 +130,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
132130 private _manuallyFloatingLabel = false ;
133131
134132 /** The subscription for closing actions (some are bound to document). */
135- private _closingActionsSubscription : Subscription ;
133+ private _closingActionsSubscription = Subscription . EMPTY ;
136134
137135 /** Subscription to viewport size changes. */
138136 private _viewportSubscription = Subscription . EMPTY ;
@@ -166,7 +164,12 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
166164 _onTouched = ( ) => { } ;
167165
168166 /** The autocomplete panel to be attached to this trigger. */
169- @Input ( 'matAutocomplete' ) autocomplete : MatAutocomplete ;
167+ @Input ( 'matAutocomplete' )
168+ get autocomplete ( ) : MatAutocomplete { return this . _autocomplete ; }
169+ set autocomplete ( value : MatAutocomplete ) {
170+ this . _autocomplete = value ;
171+ this . _detachOverlay ( ) ;
172+ }
170173
171174 /**
172175 * Reference relative to which to position the autocomplete panel.
@@ -190,8 +193,8 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
190193 this . _autocompleteDisabled = coerceBooleanProperty ( value ) ;
191194 }
192195
193- constructor ( private _element : ElementRef < HTMLInputElement > , private _overlay : Overlay ,
194- private _viewContainerRef : ViewContainerRef ,
196+ constructor ( private _element : ElementRef < HTMLInputElement > ,
197+ private _overlay : Overlay ,
195198 private _zone : NgZone ,
196199 private _changeDetectorRef : ChangeDetectorRef ,
197200 @Inject ( MAT_AUTOCOMPLETE_SCROLL_STRATEGY ) scrollStrategy : any ,
@@ -246,12 +249,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
246249 this . autocomplete . closed . emit ( ) ;
247250 }
248251
249- this . autocomplete . _isOpen = this . _overlayAttached = false ;
252+ this . autocomplete . _isOpen = false ;
253+ this . _detachOverlay ( ) ;
250254
251- if ( this . _overlayRef && this . _overlayRef . hasAttached ( ) ) {
252- this . _overlayRef . detach ( ) ;
253- this . _closingActionsSubscription . unsubscribe ( ) ;
254- }
255255
256256 // Note that in some cases this can end up being called after the component is destroyed.
257257 // Add a check to ensure that we don't try to run change detection on a destroyed view.
@@ -570,7 +570,6 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
570570 }
571571
572572 if ( ! this . _overlayRef ) {
573- this . _portal = new TemplatePortal ( this . autocomplete . template , this . _viewContainerRef ) ;
574573 this . _overlayRef = this . _overlay . create ( this . _getOverlayConfig ( ) ) ;
575574
576575 // Use the `keydownEvents` in order to take advantage of
@@ -597,7 +596,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
597596 }
598597
599598 if ( this . _overlayRef && ! this . _overlayRef . hasAttached ( ) ) {
600- this . _overlayRef . attach ( this . _portal ) ;
599+ this . _overlayRef . attach ( this . autocomplete . _portal ) ;
601600 this . _closingActionsSubscription = this . _subscribeToClosingActions ( ) ;
602601 }
603602
@@ -613,6 +612,14 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
613612 }
614613 }
615614
615+ private _detachOverlay ( ) {
616+ this . _overlayAttached = false ;
617+ this . _closingActionsSubscription . unsubscribe ( ) ;
618+ if ( this . _overlayRef ) {
619+ this . _overlayRef . detach ( ) ;
620+ }
621+ }
622+
616623 private _getOverlayConfig ( ) : OverlayConfig {
617624 return new OverlayConfig ( {
618625 positionStrategy : this . _getOverlayPosition ( ) ,
0 commit comments