@@ -15,7 +15,6 @@ import {
15
15
PositionStrategy ,
16
16
ScrollStrategy ,
17
17
} from '@angular/cdk/overlay' ;
18
- import { TemplatePortal } from '@angular/cdk/portal' ;
19
18
import { DOCUMENT } from '@angular/common' ;
20
19
import { filter , take , switchMap , delay , tap , map } from 'rxjs/operators' ;
21
20
import {
@@ -30,7 +29,6 @@ import {
30
29
NgZone ,
31
30
OnDestroy ,
32
31
Optional ,
33
- ViewContainerRef ,
34
32
} from '@angular/core' ;
35
33
import { ViewportRuler } from '@angular/cdk/scrolling' ;
36
34
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
@@ -117,9 +115,9 @@ export function getMatAutocompleteMissingPanelError(): Error {
117
115
} )
118
116
export class MatAutocompleteTrigger implements ControlValueAccessor , OnDestroy {
119
117
private _overlayRef : OverlayRef | null ;
120
- private _portal : TemplatePortal ;
121
118
private _componentDestroyed = false ;
122
119
private _autocompleteDisabled = false ;
120
+ private _autocomplete : MatAutocomplete ;
123
121
private _scrollStrategy : ( ) => ScrollStrategy ;
124
122
125
123
/** 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 {
132
130
private _manuallyFloatingLabel = false ;
133
131
134
132
/** The subscription for closing actions (some are bound to document). */
135
- private _closingActionsSubscription : Subscription ;
133
+ private _closingActionsSubscription = Subscription . EMPTY ;
136
134
137
135
/** Subscription to viewport size changes. */
138
136
private _viewportSubscription = Subscription . EMPTY ;
@@ -166,7 +164,12 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
166
164
_onTouched = ( ) => { } ;
167
165
168
166
/** 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
+ }
170
173
171
174
/**
172
175
* Reference relative to which to position the autocomplete panel.
@@ -190,8 +193,8 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
190
193
this . _autocompleteDisabled = coerceBooleanProperty ( value ) ;
191
194
}
192
195
193
- constructor ( private _element : ElementRef < HTMLInputElement > , private _overlay : Overlay ,
194
- private _viewContainerRef : ViewContainerRef ,
196
+ constructor ( private _element : ElementRef < HTMLInputElement > ,
197
+ private _overlay : Overlay ,
195
198
private _zone : NgZone ,
196
199
private _changeDetectorRef : ChangeDetectorRef ,
197
200
@Inject ( MAT_AUTOCOMPLETE_SCROLL_STRATEGY ) scrollStrategy : any ,
@@ -246,12 +249,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
246
249
this . autocomplete . closed . emit ( ) ;
247
250
}
248
251
249
- this . autocomplete . _isOpen = this . _overlayAttached = false ;
252
+ this . autocomplete . _isOpen = false ;
253
+ this . _detachOverlay ( ) ;
250
254
251
- if ( this . _overlayRef && this . _overlayRef . hasAttached ( ) ) {
252
- this . _overlayRef . detach ( ) ;
253
- this . _closingActionsSubscription . unsubscribe ( ) ;
254
- }
255
255
256
256
// Note that in some cases this can end up being called after the component is destroyed.
257
257
// 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 {
570
570
}
571
571
572
572
if ( ! this . _overlayRef ) {
573
- this . _portal = new TemplatePortal ( this . autocomplete . template , this . _viewContainerRef ) ;
574
573
this . _overlayRef = this . _overlay . create ( this . _getOverlayConfig ( ) ) ;
575
574
576
575
// Use the `keydownEvents` in order to take advantage of
@@ -597,7 +596,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
597
596
}
598
597
599
598
if ( this . _overlayRef && ! this . _overlayRef . hasAttached ( ) ) {
600
- this . _overlayRef . attach ( this . _portal ) ;
599
+ this . _overlayRef . attach ( this . autocomplete . _portal ) ;
601
600
this . _closingActionsSubscription = this . _subscribeToClosingActions ( ) ;
602
601
}
603
602
@@ -613,6 +612,14 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
613
612
}
614
613
}
615
614
615
+ private _detachOverlay ( ) {
616
+ this . _overlayAttached = false ;
617
+ this . _closingActionsSubscription . unsubscribe ( ) ;
618
+ if ( this . _overlayRef ) {
619
+ this . _overlayRef . detach ( ) ;
620
+ }
621
+ }
622
+
616
623
private _getOverlayConfig ( ) : OverlayConfig {
617
624
return new OverlayConfig ( {
618
625
positionStrategy : this . _getOverlayPosition ( ) ,
0 commit comments