Skip to content

Commit 17c357f

Browse files
authored
fix(google-maps): info window not opening if no anchor is passed in (#21014)
Some time ago we added logic to prevent the info window from reopening if the same anchor is passed in. This ended up breaking the use case where a window is opened without an anchor, because the window always starts off without one. Fixes #21013.
1 parent 5e9cc5f commit 17c357f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/google-maps/map-info-window/map-info-window.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ describe('MapInfoWindow', () => {
205205
expect(addSpy).toHaveBeenCalledWith('zindex_changed', jasmine.any(Function));
206206
subscription.unsubscribe();
207207
});
208+
209+
it('should be able to open an info window without passing in an anchor', () => {
210+
const infoWindowSpy = createInfoWindowSpy({});
211+
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
212+
213+
const fixture = TestBed.createComponent(TestApp);
214+
const infoWindowComponent = fixture.debugElement.query(By.directive(
215+
MapInfoWindow))!.injector.get<MapInfoWindow>(MapInfoWindow);
216+
fixture.detectChanges();
217+
218+
infoWindowComponent.open();
219+
expect(infoWindowSpy.open).toHaveBeenCalledTimes(1);
220+
});
221+
208222
});
209223

210224
@Component({

src/google-maps/map-info-window/map-info-window.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ export class MapInfoWindow implements OnInit, OnDestroy {
175175
this._assertInitialized();
176176
const anchorObject = anchor ? anchor.getAnchor() : undefined;
177177

178-
// Prevent the info window from initializing if trying to reopen on the same anchor.
179-
if (this.infoWindow.get('anchor') !== anchorObject) {
178+
// Prevent the info window from initializing when trying to reopen on the same anchor.
179+
// Note that when the window is opened for the first time, the anchor will always be
180+
// undefined. If that's the case, we have to allow it to open in order to handle the
181+
// case where the window doesn't have an anchor, but is placed at a particular position.
182+
if (this.infoWindow.get('anchor') !== anchorObject || !anchorObject) {
180183
this._elementRef.nativeElement.style.display = '';
181184
this.infoWindow.open(this._googleMap.googleMap, anchorObject);
182185
}

0 commit comments

Comments
 (0)