@@ -15,6 +15,7 @@ import {
1515 PositionStrategy ,
1616 ScrollStrategy ,
1717} from '@angular/cdk/overlay' ;
18+ import { TemplatePortal } from '@angular/cdk/portal' ;
1819import { DOCUMENT } from '@angular/common' ;
1920import { filter , take , switchMap , delay , tap , map } from 'rxjs/operators' ;
2021import {
@@ -29,6 +30,7 @@ import {
2930 NgZone ,
3031 OnDestroy ,
3132 Optional ,
33+ ViewContainerRef ,
3234} from '@angular/core' ;
3335import { ViewportRuler } from '@angular/cdk/scrolling' ;
3436import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
@@ -115,9 +117,9 @@ export function getMatAutocompleteMissingPanelError(): Error {
115117} )
116118export class MatAutocompleteTrigger implements ControlValueAccessor , OnDestroy {
117119 private _overlayRef : OverlayRef | null ;
120+ private _portal : TemplatePortal ;
118121 private _componentDestroyed = false ;
119122 private _autocompleteDisabled = false ;
120- private _autocomplete : MatAutocomplete ;
121123 private _scrollStrategy : ( ) => ScrollStrategy ;
122124
123125 /** Old value of the native input. Used to work around issues with the `input` event on IE. */
@@ -130,7 +132,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
130132 private _manuallyFloatingLabel = false ;
131133
132134 /** The subscription for closing actions (some are bound to document). */
133- private _closingActionsSubscription = Subscription . EMPTY ;
135+ private _closingActionsSubscription : Subscription ;
134136
135137 /** Subscription to viewport size changes. */
136138 private _viewportSubscription = Subscription . EMPTY ;
@@ -164,12 +166,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
164166 _onTouched = ( ) => { } ;
165167
166168 /** The autocomplete panel to be attached to this trigger. */
167- @Input ( 'matAutocomplete' )
168- get autocomplete ( ) : MatAutocomplete { return this . _autocomplete ; }
169- set autocomplete ( value : MatAutocomplete ) {
170- this . _autocomplete = value ;
171- this . _detachOverlay ( ) ;
172- }
169+ @Input ( 'matAutocomplete' ) autocomplete : MatAutocomplete ;
173170
174171 /**
175172 * Reference relative to which to position the autocomplete panel.
@@ -193,8 +190,8 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
193190 this . _autocompleteDisabled = coerceBooleanProperty ( value ) ;
194191 }
195192
196- constructor ( private _element : ElementRef < HTMLInputElement > ,
197- private _overlay : Overlay ,
193+ constructor ( private _element : ElementRef < HTMLInputElement > , private _overlay : Overlay ,
194+ private _viewContainerRef : ViewContainerRef ,
198195 private _zone : NgZone ,
199196 private _changeDetectorRef : ChangeDetectorRef ,
200197 @Inject ( MAT_AUTOCOMPLETE_SCROLL_STRATEGY ) scrollStrategy : any ,
@@ -249,9 +246,12 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
249246 this . autocomplete . closed . emit ( ) ;
250247 }
251248
252- this . autocomplete . _isOpen = false ;
253- this . _detachOverlay ( ) ;
249+ this . autocomplete . _isOpen = this . _overlayAttached = false ;
254250
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,6 +570,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
570570 }
571571
572572 if ( ! this . _overlayRef ) {
573+ this . _portal = new TemplatePortal ( this . autocomplete . template , this . _viewContainerRef ) ;
573574 this . _overlayRef = this . _overlay . create ( this . _getOverlayConfig ( ) ) ;
574575
575576 // Use the `keydownEvents` in order to take advantage of
@@ -596,7 +597,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
596597 }
597598
598599 if ( this . _overlayRef && ! this . _overlayRef . hasAttached ( ) ) {
599- this . _overlayRef . attach ( this . autocomplete . _portal ) ;
600+ this . _overlayRef . attach ( this . _portal ) ;
600601 this . _closingActionsSubscription = this . _subscribeToClosingActions ( ) ;
601602 }
602603
@@ -612,14 +613,6 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
612613 }
613614 }
614615
615- private _detachOverlay ( ) {
616- this . _overlayAttached = false ;
617- this . _closingActionsSubscription . unsubscribe ( ) ;
618- if ( this . _overlayRef ) {
619- this . _overlayRef . detach ( ) ;
620- }
621- }
622-
623616 private _getOverlayConfig ( ) : OverlayConfig {
624617 return new OverlayConfig ( {
625618 positionStrategy : this . _getOverlayPosition ( ) ,
0 commit comments