ngFor not updating when @Input updated from google map marker event

别等时光非礼了梦想. 提交于 2019-12-08 04:54:30

From the User Input dev guide:

Angular only updates the bindings (and therefore the screen) if we do something in response to asynchronous events such as keystrokes.

I'm guessing you don't have an asynchronous event when the markers array is updated, hence nothing is causing Angular to run its change detection algorithm.

Update: The issue was not that there wasn't an asynchronous event, the issue was that the asynchronous event (google.maps.Marker.addListener()) is not known by Angular, hence Zone does not monkey patch it, hence Angular doesn't run its change detection algorithm when the data changes. The solution is to emit the event inside Zone's run() function:

marker.addListener('click', () => {
   this.zone.run(() => {
      this.markerClickEvent.next({data:{name: 'another'}});
   });
});

This will cause Angular to run its change detection algorithm, notice the change, and update the view.

The reason setInterval() works is because it is monkey patched by Zone.

See DiSol's plunker for more details.
If you want to learn more about Zone, watch Miško's video.
See also NgZone API doc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!