File tree 5 files changed +43
-14
lines changed
5 files changed +43
-14
lines changed Original file line number Diff line number Diff line change @@ -70,7 +70,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
70
70
}
71
71
72
72
ngOnDestroy ( ) {
73
- this . dispose ( ) ;
73
+ super . dispose ( ) ;
74
74
}
75
75
76
76
/**
@@ -93,7 +93,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
93
93
componentFactory , viewContainerRef . length ,
94
94
portal . injector || viewContainerRef . parentInjector ) ;
95
95
96
- this . setDisposeFn ( ( ) => ref . destroy ( ) ) ;
96
+ super . setDisposeFn ( ( ) => ref . destroy ( ) ) ;
97
97
return ref ;
98
98
}
99
99
@@ -105,7 +105,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
105
105
portal . setAttachedHost ( this ) ;
106
106
107
107
this . _viewContainerRef . createEmbeddedView ( portal . templateRef ) ;
108
- this . setDisposeFn ( ( ) => this . _viewContainerRef . clear ( ) ) ;
108
+ super . setDisposeFn ( ( ) => this . _viewContainerRef . clear ( ) ) ;
109
109
110
110
// TODO(jelbourn): return locals from view
111
111
return new Map < string , any > ( ) ;
@@ -114,11 +114,11 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
114
114
/** Detaches the currently attached Portal (if there is one) and attaches the given Portal. */
115
115
private _replaceAttachedPortal ( p : Portal < any > ) : void {
116
116
if ( this . hasAttached ( ) ) {
117
- this . detach ( ) ;
117
+ super . detach ( ) ;
118
118
}
119
119
120
120
if ( p ) {
121
- this . attach ( p ) ;
121
+ super . attach ( p ) ;
122
122
this . _portal = p ;
123
123
}
124
124
}
Original file line number Diff line number Diff line change @@ -278,6 +278,17 @@ describe('Portals', () => {
278
278
expect ( someDomElement . innerHTML )
279
279
. toBe ( '' , 'Expected the DomPortalHost to be empty after detach' ) ;
280
280
} ) ;
281
+
282
+ it ( 'should call the dispose function even if the host has no attached content' , ( ) => {
283
+ let spy = jasmine . createSpy ( 'host dispose spy' ) ;
284
+
285
+ expect ( host . hasAttached ( ) ) . toBe ( false , 'Expected host not to have attached content.' ) ;
286
+
287
+ host . setDisposeFn ( spy ) ;
288
+ host . dispose ( ) ;
289
+
290
+ expect ( spy ) . toHaveBeenCalled ( ) ;
291
+ } ) ;
281
292
} ) ;
282
293
} ) ;
283
294
Original file line number Diff line number Diff line change @@ -162,12 +162,12 @@ export abstract class BasePortalHost implements PortalHost {
162
162
private _isDisposed : boolean = false ;
163
163
164
164
/** Whether this host has an attached portal. */
165
- hasAttached ( ) {
166
- return this . _attachedPortal != null ;
165
+ hasAttached ( ) : boolean {
166
+ return ! ! this . _attachedPortal ;
167
167
}
168
168
169
169
attach ( portal : Portal < any > ) : any {
170
- if ( portal == null ) {
170
+ if ( ! portal ) {
171
171
throw new NullPortalError ( ) ;
172
172
}
173
173
@@ -195,24 +195,31 @@ export abstract class BasePortalHost implements PortalHost {
195
195
abstract attachTemplatePortal ( portal : TemplatePortal ) : Map < string , any > ;
196
196
197
197
detach ( ) : void {
198
- if ( this . _attachedPortal ) { this . _attachedPortal . setAttachedHost ( null ) ; }
199
-
200
- this . _attachedPortal = null ;
201
- if ( this . _disposeFn != null ) {
202
- this . _disposeFn ( ) ;
203
- this . _disposeFn = null ;
198
+ if ( this . _attachedPortal ) {
199
+ this . _attachedPortal . setAttachedHost ( null ) ;
200
+ this . _attachedPortal = null ;
204
201
}
202
+
203
+ this . _invokeDisposeFn ( ) ;
205
204
}
206
205
207
206
dispose ( ) {
208
207
if ( this . hasAttached ( ) ) {
209
208
this . detach ( ) ;
210
209
}
211
210
211
+ this . _invokeDisposeFn ( ) ;
212
212
this . _isDisposed = true ;
213
213
}
214
214
215
215
setDisposeFn ( fn : ( ) => void ) {
216
216
this . _disposeFn = fn ;
217
217
}
218
+
219
+ private _invokeDisposeFn ( ) {
220
+ if ( this . _disposeFn ) {
221
+ this . _disposeFn ( ) ;
222
+ this . _disposeFn = null ;
223
+ }
224
+ }
218
225
}
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ export class MdDialogRef<T> {
27
27
this . _overlayRef . dispose ( ) ;
28
28
this . _afterClosed . next ( dialogResult ) ;
29
29
this . _afterClosed . complete ( ) ;
30
+ this . componentInstance = null ;
30
31
}
31
32
32
33
/**
Original file line number Diff line number Diff line change @@ -271,6 +271,16 @@ describe('MdDialog', () => {
271
271
expect ( overlayContainerElement . querySelectorAll ( 'md-dialog-container' ) . length ) . toBe ( 0 ) ;
272
272
} ) ;
273
273
274
+ it ( 'should not keep a reference to the component instance after the dialog is closed' , ( ) => {
275
+ let dialogRef = dialog . open ( PizzaMsg ) ;
276
+
277
+ expect ( dialogRef . componentInstance ) . toBeTruthy ( ) ;
278
+
279
+ dialogRef . close ( ) ;
280
+
281
+ expect ( dialogRef . componentInstance ) . toBeFalsy ( 'Expected reference to have been cleared.' ) ;
282
+ } ) ;
283
+
274
284
describe ( 'disableClose option' , ( ) => {
275
285
it ( 'should prevent closing via clicks on the backdrop' , ( ) => {
276
286
dialog . open ( PizzaMsg , {
You can’t perform that action at this time.
0 commit comments