Angular Leaflet弹出窗口点击无效?

3
我已经在我的Angular 6应用程序中实现了@asymmetrik/ngx-leaflet,除了弹出窗口之外,一切都正常。在我展示地图上的一些标记后,它显示的像图片中的那样:marker popup 我想在用户点击按钮时更改路由,但是使用Angular的(click)事件不会触发单击事件,我已经在onClick中实现了内联函数JavaScript,它在我单击弹出窗口之前调用了很多次,这样就无法正常工作。代码如下:
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]);
      //   })
      // })
    )};
  });

感谢您的请求。
2个回答

6

我为我正在开发的应用程序做了类似的事情,并且按照以下方式使其工作:

.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);
}

2

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');
  }

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接