How could I call a Angular2 function from a Google Map infowindow?

后端 未结 5 1899
小鲜肉
小鲜肉 2021-02-10 17:17

Google map is integrated the javascript way and I want to call a angular2 function inside an infowindow like the following code. Take a look at infoContent for the

5条回答
  •  长发绾君心
    2021-02-10 17:53

    update (from comment)

    You can do it with ElementRef and Renderer but that is not the problem. The problem is to get a reference (direct DOM element reference or ElementRef) of the button. You can inject private elRef:ElementRef and use this.elRef.nativeElement.querySelector('infowindow button') or similar but if you access elRef.nativeElement you're out of the realm of platform neutrality again because nativeElement should not be accessed directly, but with the limitation of the Renderer of only being able to call methods but never to get a result in return, there is only so much you can do.

    I would use a method like shown in Trigger event with infoWindow or InfoBox on click Google Map API V3 and then check the event.target if an element you are interested in was clicked.

    original

    If you want to access this.map in the event handler you should use arrow functions instead

      google.maps.event.addListener(marker, 'click', ((marker, i) => { // <<<===
        return () => { // <<<===
          infowindow.setContent(infoContent);
          infowindow.open(this.map, marker);
        };
      })(marker, i)
    

    otherwise this. won't point to the current class instance

提交回复
热议问题