@@ -22,12 +22,23 @@ import {BasePortalOutlet, ComponentPortal, DomPortal, TemplatePortal} from './po
22
22
export class DomPortalOutlet extends BasePortalOutlet {
23
23
private _document : Document ;
24
24
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
+ */
25
36
constructor (
26
37
/** Element into which the content is projected. */
27
38
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 ,
31
42
32
43
/**
33
44
* @deprecated `_document` Parameter to be made required.
@@ -45,7 +56,12 @@ export class DomPortalOutlet extends BasePortalOutlet {
45
56
* @returns Reference to the created component.
46
57
*/
47
58
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
+
49
65
const componentFactory = resolver . resolveComponentFactory ( portal . component ) ;
50
66
let componentRef : ComponentRef < T > ;
51
67
@@ -62,10 +78,16 @@ export class DomPortalOutlet extends BasePortalOutlet {
62
78
63
79
this . setDisposeFn ( ( ) => componentRef . destroy ( ) ) ;
64
80
} 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 ) ;
67
89
this . setDisposeFn ( ( ) => {
68
- this . _appRef . detachView ( componentRef . hostView ) ;
90
+ this . _appRef ! . detachView ( componentRef . hostView ) ;
69
91
componentRef . destroy ( ) ;
70
92
} ) ;
71
93
}
0 commit comments