【问题标题】:Angular Leaflet Popup Click not Working?角传单弹出点击不起作用?
【发布时间】:2018-12-12 15:02:20
【问题描述】:

我已经在我的 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]);
      //   })
      // })
    )};
  });

谢谢。

【问题讨论】:

    标签: angular leaflet


    【解决方案1】:

    我为我正在开发的应用程序做了一个类似的事情,并让它像这样工作:

    .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

      【讨论】:

        猜你喜欢
        • 2018-12-17
        • 1970-01-01
        • 2018-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-11
        • 2020-03-04
        相关资源
        最近更新 更多