Skip to content

Commit

Permalink
fix(overlay): attempting to position overlay if it was detached immed…
Browse files Browse the repository at this point in the history
…iately after being attached (#9507)

Adds a check that ensures that the `OverlayRef` doesn't attempt to position itself if it was detached before the zone managed to stabilize. This manifested itself as an issue in the select where it could end up throwing an error, because the relevant nodes are no longer in the DOM.

Fixes #9406.
  • Loading branch information
crisbeto authored and jelbourn committed Jan 25, 2018
1 parent 403ebbd commit c7ad145
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ export class OverlayRef implements PortalOutlet {
// before attempting to position it, as the position may depend on the size of the rendered
// content.
this._ngZone.onStable.asObservable().pipe(take(1)).subscribe(() => {
this.updatePosition();
// The overlay could've been detached before the zone has stabilized.
if (this.hasAttached()) {
this.updatePosition();
}
});

// Enable pointer events for the overlay pane element.
Expand Down
16 changes: 16 additions & 0 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,22 @@ describe('Overlay', () => {

expect(overlayContainerElement.querySelectorAll('.fake-positioned').length).toBe(1);
}));

it('should not apply the position if it detaches before the zone stabilizes', fakeAsync(() => {
config.positionStrategy = new FakePositionStrategy();

const overlayRef = overlay.create(config);

spyOn(config.positionStrategy, 'apply');

overlayRef.attach(componentPortal);
overlayRef.detach();
viewContainerFixture.detectChanges();
tick();

expect(config.positionStrategy.apply).not.toHaveBeenCalled();
}));

});

describe('size', () => {
Expand Down

0 comments on commit c7ad145

Please sign in to comment.