0

I have Google maps on the home page and I'm trying to route the user to another page when closing the info window that appears on the map:

// Google map info window code snippet from a @Directive
infowindow.addListener('closeclick', () => {
  this.router.navigate(['/users', user.id]);
});

// Component code
ngOnInit() {
  this.user$ = this.route.params.switchMap((params: Params) => this.service.getUser(params['id']));
}

// UserService
getUser(id: String): Observable < User > {
  return this.apollo.watchQuery < GetUserQueryResult > ({
    query: GetUserQuery,
    variables: {
      id: id
    }
  }).map(({
    data
  }) => data.User);
}
<div *ngIf="user$ | async as user" class="container">
  <p>user.name</p>
</div>

The problem is that when I get to the user page, the div never gets displayed except when actually click on the page once. What's wrong?

Sammy
  • 3,395
  • 7
  • 49
  • 95

1 Answers1

0

Running handler code inside angular zone should help you

import { NgZone } from '@angular/core';

...
constructor(private zone: NgZone) {}

infowindow.addListener('closeclick', () => {
  this.zone.run(() => this.router.navigate(['/users', user.id]));
});

See also

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • That, of course, worked perfectly. Thank you, and sorry for not having seen the other link before. – Sammy Jun 10 '17 at 11:42