Arsen Cenko
Arsen Cenko

Reputation: 95

Angular Leaflet Popup Click not Working?

I have implemented @asymmetrik/ngx-leaflet in my angular 6 app and everything works fine except the popup. Its showing like in the picture after i have shown some markers in the map: marker popup

I want to change the route when the user clicks in the button, but with the (click) event from angular its not triggerin the click event, i have implemented inline function javascript in onClick and its calling before i click on the popup as much as map have markers ! So its not working. Code is like this :

this.listOfStations.map(station => {
    if(Object.keys(station.location).length > 0){
      this.markers.push(marker([station.location.coordinates[0],station.location.coordinates[1]],{
        icon: icon({
          iconSize: [ 25, 41 ],
          iconAnchor: [ 13, 41 ],
          iconUrl: 'assets/marker-icon.png',
          shadowUrl: 'assets/marker-shadow.png'
       })
      })
      .bindPopup(`
        <div align='center'>
          <p style='font-size:18px;font-weight:bold'>Station: ${station.stationCode}</p>
          <p style='font-size:14px;font-weight:italic'>${station.description}</p>
          <a class='btn btn-xs btn-primary button-me' (click)="${this.consoleThis()}">View</a>
        </div>
      `)
      // .on('click', (e) => {
      //   this.zone.run(() => {
      //     this.router.navigate(['/dashboard/station/' + station.stationCode]);
      //   })
      // })
    )};
  });

Thanks.

Upvotes: 3

Views: 3798

Answers (2)

Answer for Angular 9:

export class MapComponent implements OnInit {

...

//whenever we do click, if element clicked has class "goOrigin" then execute this.goOrigin
  @HostListener('document:click', ['$event']) 
  clickout(event) 
  { 
    if(event.target.classList.contains("goOrigin"))
      this.goOrigin(); 
  }

...

//Just bind a text to your marker
let text: string = "<a href='#' class='goOrigin'>Usar de origen ></a>";
this.markerPoint = L.marker([latitude, longitude]);
this.markerPoint.addTo(this.map).bindPopup(text);

...

  goOrigin()
  {
    console.log('origen');
  }

Source: https://github.com/Asymmetrik/ngx-leaflet/issues/60#issuecomment-493716598

Upvotes: 2

juhomontonen
juhomontonen

Reputation: 76

I made a similiar thing for an app im working on and got it working like this:

.bindPopup(`
    <div align='center'>
    <a class="btn btn-primary button-raised partner-link" data-partnerId="${markerinfo.partnerId}">
      ${markerinfo.partnerName}</a>
      <p style='font-size:14px;margin: 5px'>${markerinfo.partnerAddress}</p>
      <img style="height: 100px; width: 170px; margin: 0" src="${markerinfo.descImg}">
      <p style="margin:5px;font-style:italic">${markerinfo.descText}</p>
    </div>
  `).addTo(map)
          .on('popupopen', function () {
            self.elementRef.nativeElement.querySelector('.partner-link')
              .addEventListener('click', (e) => {
                const partnerId = e.target.getAttribute('data-partnerId');
                self.showPartner(partnerId);
              });
          })

showPartner(partnerId) {
// this.router.navigate(['/app/partners' + '/' + partnerId]);
console.log('going to partner: ' + partnerId);
}

Upvotes: 6

Related Questions