11import {
2- ComponentFactoryResolver ,
32 ComponentRef ,
43 Directive ,
54 ElementRef ,
@@ -95,7 +94,6 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
9594 @Inject ( DOCUMENT ) private document : any ,
9695 private renderer : Renderer2 ,
9796 private hostElement : ElementRef ,
98- private componentFactoryResolver : ComponentFactoryResolver ,
9997 private viewContainerRef : ViewContainerRef ,
10098 private listenersService : ListenersService
10199 ) { }
@@ -150,9 +148,8 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
150148
151149 private createPopoverElement ( ) : void {
152150 if ( ! this . popoverRef ) {
153- const popoverComponent =
154- this . componentFactoryResolver . resolveComponentFactory ( PopoverComponent ) ;
155- this . popoverRef = popoverComponent . create ( this . viewContainerRef . injector ) ;
151+ this . popoverRef = this . viewContainerRef . createComponent < PopoverComponent > ( PopoverComponent ) ;
152+ // this.viewContainerRef.detach();
156153 }
157154 }
158155
@@ -162,8 +159,8 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
162159 // @ts -ignore
163160 this . popoverRef = undefined ;
164161 this . popperInstance ?. destroy ( ) ;
165- this . viewContainerRef . detach ( ) ;
166- this . viewContainerRef . clear ( ) ;
162+ this . viewContainerRef ? .detach ( ) ;
163+ this . viewContainerRef ? .clear ( ) ;
167164 }
168165
169166 private addPopoverElement ( ) : void {
@@ -172,36 +169,46 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
172169 }
173170 this . popoverRef . instance . content = this . content ;
174171 this . popover = this . popoverRef . location . nativeElement ;
172+ this . renderer . addClass ( this . popover , 'd-none' ) ;
175173 this . renderer . addClass ( this . popover , 'fade' ) ;
176174
175+ this . popperInstance ?. destroy ( ) ;
176+
177177 setTimeout ( ( ) => {
178178 this . popperInstance = createPopper (
179179 this . hostElement . nativeElement ,
180180 this . popover ,
181181 { ...this . popperOptions }
182182 ) ;
183183 this . viewContainerRef . insert ( this . popoverRef . hostView ) ;
184+ this . renderer . appendChild ( this . document . body , this . popover ) ;
185+ if ( ! this . visible ) {
186+ this . removePopoverElement ( ) ;
187+ return ;
188+ }
184189 setTimeout ( ( ) => {
185190 this . popoverId = this . getUID ( 'popover' ) ;
186191 this . popoverRef . instance . id = this . popoverId ;
192+ if ( ! this . visible ) {
193+ this . removePopoverElement ( ) ;
194+ return ;
195+ }
196+ this . renderer . removeClass ( this . popover , 'd-none' ) ;
187197 this . popoverRef . instance . visible = this . visible ;
188- this . renderer . appendChild ( this . document . body , this . popover ) ;
189198 this . popperInstance . forceUpdate ( ) ;
190- // this.popoverRef.changeDetectorRef.markForCheck();
191199 } , 100 ) ;
192200 } ) ;
193201 }
194202
195203 private removePopoverElement ( ) : void {
204+ this . popoverId = '' ;
196205 if ( ! this . popoverRef ) {
197206 return ;
198207 }
199208 this . popoverRef . instance . visible = this . visible ;
200209 this . popoverRef . instance . id = undefined ;
201210 setTimeout ( ( ) => {
202211 this . viewContainerRef . detach ( ) ;
203- this . popperInstance ?. destroy ( ) ;
204- this . popoverId = '' ;
205212 } , 300 ) ;
206213 }
207214}
0 commit comments