简体   繁体   English

角度2+ ng如果在单击页面后未显示div

[英]angular 2+ ngIf div not appearing except after clicking on page

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地图,并且在关闭显示在地图上的信息窗口时试图将用户路由到另一个页面:

 // 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. 问题是,当我进入用户页面时,div永远不会显示,除非实际单击一次该页面。 What's wrong? 怎么了?

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 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM