diff --git a/src/lib/core/overlay/overlay-directives.spec.ts b/src/lib/core/overlay/overlay-directives.spec.ts
index 8e7c98bd6918..5c81c480dc8c 100644
--- a/src/lib/core/overlay/overlay-directives.spec.ts
+++ b/src/lib/core/overlay/overlay-directives.spec.ts
@@ -167,6 +167,23 @@ describe('Overlay directives', () => {
`Expected directive to emit an instance of ConnectedOverlayPositionChange.`);
});
+ it('should emit attach and detach appropriately', () => {
+ expect(fixture.componentInstance.attachHandler).not.toHaveBeenCalled();
+ expect(fixture.componentInstance.detachHandler).not.toHaveBeenCalled();
+ fixture.componentInstance.isOpen = true;
+ fixture.detectChanges();
+
+ expect(fixture.componentInstance.attachHandler).toHaveBeenCalled();
+ expect(fixture.componentInstance.attachResult)
+ .toEqual(jasmine.any(HTMLElement),
+ `Expected pane to be populated with HTML elements when attach was called.`);
+ expect(fixture.componentInstance.detachHandler).not.toHaveBeenCalled();
+
+ fixture.componentInstance.isOpen = false;
+ fixture.detectChanges();
+ expect(fixture.componentInstance.detachHandler).toHaveBeenCalled();
+ });
+
});
});
@@ -178,7 +195,8 @@ describe('Overlay directives', () => {
+ (positionChange)="positionChangeHandler($event)" (attach)="attachHandler()"
+ (detach)="detachHandler()">
Menu content
`,
})
@@ -191,6 +209,12 @@ class ConnectedOverlayDirectiveTest {
hasBackdrop: boolean;
backdropClicked = false;
positionChangeHandler = jasmine.createSpy('positionChangeHandler');
+ attachHandler = jasmine.createSpy('attachHandler').and.callFake(() => {
+ this.attachResult =
+ this.connectedOverlayDirective.overlayRef.overlayElement.querySelector('p') as HTMLElement;
+ });
+ detachHandler = jasmine.createSpy('detachHandler');
+ attachResult: HTMLElement;
@ViewChild(ConnectedOverlayDirective) connectedOverlayDirective: ConnectedOverlayDirective;
}
diff --git a/src/lib/core/overlay/overlay-directives.ts b/src/lib/core/overlay/overlay-directives.ts
index ea1bbade35c7..5255646d5096 100644
--- a/src/lib/core/overlay/overlay-directives.ts
+++ b/src/lib/core/overlay/overlay-directives.ts
@@ -110,6 +110,8 @@ export class ConnectedOverlayDirective implements OnDestroy {
/** Event emitted when the backdrop is clicked. */
@Output() backdropClick = new EventEmitter();
@Output() positionChange = new EventEmitter();
+ @Output() attach = new EventEmitter();
+ @Output() detach = new EventEmitter();
// TODO(jelbourn): inputs for size, scroll behavior, animation, etc.
@@ -205,6 +207,7 @@ export class ConnectedOverlayDirective implements OnDestroy {
if (!this._overlayRef.hasAttached()) {
this._overlayRef.attach(this._templatePortal);
+ this.attach.emit();
}
if (this.hasBackdrop) {
@@ -218,6 +221,7 @@ export class ConnectedOverlayDirective implements OnDestroy {
private _detachOverlay() {
if (this._overlayRef) {
this._overlayRef.detach();
+ this.detach.emit();
}
if (this._backdropSubscription) {
diff --git a/src/lib/core/overlay/overlay-ref.ts b/src/lib/core/overlay/overlay-ref.ts
index f2cd0e828451..0fb3b758b372 100644
--- a/src/lib/core/overlay/overlay-ref.ts
+++ b/src/lib/core/overlay/overlay-ref.ts
@@ -19,6 +19,11 @@ export class OverlayRef implements PortalHost {
private _state: OverlayState,
private _ngZone: NgZone) { }
+ /** The overlay's HTML element */
+ get overlayElement(): HTMLElement {
+ return this._pane;
+ }
+
attach(portal: Portal): any {
if (this._state.hasBackdrop) {
this._attachBackdrop();