@@ -17,6 +17,8 @@ import {
17
17
OnDestroy ,
18
18
OnInit ,
19
19
Input ,
20
+ EventEmitter ,
21
+ Output ,
20
22
} from '@angular/core' ;
21
23
import { Portal , TemplatePortal , ComponentPortal , BasePortalOutlet } from './portal' ;
22
24
@@ -35,6 +37,11 @@ export class CdkPortal extends TemplatePortal<any> {
35
37
}
36
38
}
37
39
40
+ /**
41
+ * Possible attached references to the CdkPortalOutlet.
42
+ */
43
+ export type CdkPortalOutletAttachedRef = ComponentRef < any > | EmbeddedViewRef < any > | null ;
44
+
38
45
39
46
/**
40
47
* Directive version of a PortalOutlet. Because the directive *is* a PortalOutlet, portals can be
@@ -52,6 +59,9 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
52
59
/** Whether the portal component is initialized. */
53
60
private _isInitialized = false ;
54
61
62
+ /** Reference to the currently-attached component/view ref. */
63
+ private _attachedRef : CdkPortalOutletAttachedRef ;
64
+
55
65
constructor (
56
66
private _componentFactoryResolver : ComponentFactoryResolver ,
57
67
private _viewContainerRef : ViewContainerRef ) {
@@ -93,13 +103,22 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
93
103
this . _attachedPortal = portal ;
94
104
}
95
105
106
+ @Output ( 'attached' ) attached : EventEmitter < CdkPortalOutletAttachedRef > =
107
+ new EventEmitter < CdkPortalOutletAttachedRef > ( ) ;
108
+
109
+ /** Component or view reference that is attached to the portal. */
110
+ get attachedRef ( ) : CdkPortalOutletAttachedRef {
111
+ return this . _attachedRef ;
112
+ }
113
+
96
114
ngOnInit ( ) {
97
115
this . _isInitialized = true ;
98
116
}
99
117
100
118
ngOnDestroy ( ) {
101
119
super . dispose ( ) ;
102
120
this . _attachedPortal = null ;
121
+ this . _attachedRef = null ;
103
122
}
104
123
105
124
/**
@@ -125,6 +144,8 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
125
144
126
145
super . setDisposeFn ( ( ) => ref . destroy ( ) ) ;
127
146
this . _attachedPortal = portal ;
147
+ this . _attachedRef = ref ;
148
+ this . attached . emit ( ref ) ;
128
149
129
150
return ref ;
130
151
}
@@ -140,6 +161,8 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
140
161
super . setDisposeFn ( ( ) => this . _viewContainerRef . clear ( ) ) ;
141
162
142
163
this . _attachedPortal = portal ;
164
+ this . _attachedRef = viewRef ;
165
+ this . attached . emit ( viewRef ) ;
143
166
144
167
return viewRef ;
145
168
}
0 commit comments