@@ -22,12 +22,23 @@ import {BasePortalOutlet, ComponentPortal, TemplatePortal, DomPortal} from './po
2222export class DomPortalOutlet extends BasePortalOutlet {
2323 private _document : Document ;
2424
25+ /**
26+ * @param outletElement Element into which the content is projected.
27+ * @param _componentFactoryResolver Used to resolve the component factory.
28+ * Only required when attaching component portals.
29+ * @param _appRef Reference to the application. Only used in component portals when there
30+ * is no `ViewContainerRef` available.
31+ * @param _defaultInjector Injector to use as a fallback when the portal being attached doesn't
32+ * have one. Only used for component portals.
33+ * @param _document Reference to the document. Used when attaching a DOM portal. Will eventually
34+ * become a required parameter.
35+ */
2536 constructor (
2637 /** Element into which the content is projected. */
2738 public outletElement : Element ,
28- private _componentFactoryResolver : ComponentFactoryResolver ,
29- private _appRef : ApplicationRef ,
30- private _defaultInjector : Injector ,
39+ private _componentFactoryResolver ? : ComponentFactoryResolver ,
40+ private _appRef ? : ApplicationRef ,
41+ private _defaultInjector ? : Injector ,
3142
3243 /**
3344 * @deprecated `_document` Parameter to be made required.
@@ -45,7 +56,12 @@ export class DomPortalOutlet extends BasePortalOutlet {
4556 * @returns Reference to the created component.
4657 */
4758 attachComponentPortal < T > ( portal : ComponentPortal < T > ) : ComponentRef < T > {
48- const resolver = portal . componentFactoryResolver || this . _componentFactoryResolver ;
59+ const resolver = ( portal . componentFactoryResolver || this . _componentFactoryResolver ) ! ;
60+
61+ if ( ( typeof ngDevMode === 'undefined' || ngDevMode ) && ! resolver ) {
62+ throw Error ( 'Cannot attach component portal to outlet without a ComponentFactoryResolver.' ) ;
63+ }
64+
4965 const componentFactory = resolver . resolveComponentFactory ( portal . component ) ;
5066 let componentRef : ComponentRef < T > ;
5167
@@ -62,10 +78,16 @@ export class DomPortalOutlet extends BasePortalOutlet {
6278
6379 this . setDisposeFn ( ( ) => componentRef . destroy ( ) ) ;
6480 } else {
65- componentRef = componentFactory . create ( portal . injector || this . _defaultInjector ) ;
66- this . _appRef . attachView ( componentRef . hostView ) ;
81+ if ( ( typeof ngDevMode === 'undefined' || ngDevMode ) && ! this . _appRef ) {
82+ throw Error ( 'Cannot attach component portal to outlet without an ApplicationRef.' ) ;
83+ }
84+
85+ componentRef = componentFactory . create (
86+ portal . injector || this . _defaultInjector || Injector . NULL ,
87+ ) ;
88+ this . _appRef ! . attachView ( componentRef . hostView ) ;
6789 this . setDisposeFn ( ( ) => {
68- this . _appRef . detachView ( componentRef . hostView ) ;
90+ this . _appRef ! . detachView ( componentRef . hostView ) ;
6991 componentRef . destroy ( ) ;
7092 } ) ;
7193 }
0 commit comments