license import { ComponentFactoryResolver , ComponentRef , EmbeddedViewRef , ApplicationRef , Injector , } from '@angular/core' ; import { BasePortalOutlet , ComponentPortal , TemplatePortal } from './portal' ; /**
 * A PortalOutlet for attaching portals to an arbitrary DOM element outside of the Angular
 * application context.
 */ export class DomPortalOutlet extends BasePortalOutlet { constructor ( /** Element into which the content is projected. */ public outletElement , private _componentFactoryResolver , private _appRef , private _defaultInjector ) { super ( ) ; } param portal returns attachComponentPortal < T > ( portal ) { const resolver = portal . componentFactoryResolver || this . _componentFactoryResolver ; const componentFactory = resolver . resolveComponentFactory ( portal . component ) ; let componentRef ; if ( portal . viewContainerRef ) { componentRef = portal . viewContainerRef . createComponent ( componentFactory , portal . viewContainerRef . length , portal . injector || portal . viewContainerRef . injector ) ; this . setDisposeFn ( ( ) => componentRef . destroy ( ) ) ; } else { componentRef = componentFactory . create ( portal . injector || this . _defaultInjector ) ; this . _appRef . attachView ( componentRef . hostView ) ; this . setDisposeFn ( ( ) => { this . _appRef . detachView ( componentRef . hostView ) ; componentRef . destroy ( ) ; } ) ; } this . outletElement . appendChild ( this . _getComponentRootNode ( componentRef ) ) ; return componentRef ; } param portal returns attachTemplatePortal < C > ( portal ) { let viewContainer = portal . viewContainerRef ; let viewRef = viewContainer . createEmbeddedView ( portal . templateRef , portal . context ) ; viewRef . detectChanges ( ) ; viewRef . rootNodes . forEach ( rootNode => this . outletElement . appendChild ( rootNode ) ) ; this . setDisposeFn ( ( ( ) => { let index = viewContainer . indexOf ( viewRef ) ; if ( index !== - 1 ) { viewContainer . remove ( index ) ; } } ) ) ; return viewRef ; } /**
     * Clears out a portal from the DOM.
     */ dispose ( ) { super . dispose ( ) ; if ( this . outletElement . parentNode != null ) { this . outletElement . parentNode . removeChild ( this . outletElement ) ; } } /** Gets the root HTMLElement for an instantiated component. */ private _getComponentRootNode ( componentRef ) { return ( componentRef . hostView as EmbeddedViewRef < any > ) . rootNodes [ 0 ] as HTMLElement ; } }