在离子2和Angular 2中添加链接到传单弹出窗口



我必须用离子2,Angular 2和打字稿构建移动应用程序。
我在应用程序中使用传单地图。
我想单击地图上的标记显示一个弹出窗口包含一个链接。
此链接在打字稿中调用函数,但在弹出窗口中不起作用。

  public goToMerchant(merchantId) {
    this.navCtrl.push(MerchantPage, { merchantId: merchantId });
  }
 var popupLink='<a (click)="goToMerchant(200)">Show</a>'
 Leaflet.marker([item.lat, item.lng])
        .bindPopup(popupLink)
        .addTo(map);

在显示弹出链接后不使用单击。
如何解决此问题?

这是您可以做的一种可能的方法:

plunker https://plnkr.co/edit/cjzrdkxjxvjt5l3qixmp?p=preview

代码:

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <div id="mapid" style="height: 180px;"></div>
    </div>
  `,
})
export class App implements AfterViewInit{
  name:string = `Angular! v${VERSION.full}`;
  myMap: any;
  constructor(private elementRef: ElementRef) {}
  goToMerchant(merchantId) {
    //this.navCtrl.push(MerchantPage, { merchantId: merchantId });
    console.log("going to merchant "+merchantId)
  }
  ngAfterViewInit(){
    // setup map
    this.myMap = L.map('mapid').setView([51.505, -0.09], 13);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {}).addTo(this.myMap);
    // note class= merchLink and data-merchId = 200
    var popupLink='<a class="merch-link" data-merchId="200">Show</a>'
    // add marker
    var marker = L.marker([51.5, -0.09])
        .bindPopup(popupLink)
        .addTo(this.myMap)
        .openPopup();
    // add event listener to newly added a.merch-link element
    this.elementRef.nativeElement.querySelector(".merch-link")
    .addEventListener('click', (e)=>
    {
      // get id from attribute
      var merchId = e.target.getAttribute("data-merchId");
      this.goToMerchant(merchId)
    }));
  }
}

即使在一开始就没有打开弹出窗口,我建议对艾哈迈德建议的答案进行小改编

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <div id="mapid" style="height: 180px;"></div>
    </div>
  `,
})
export class App implements AfterViewInit{
  name:string = `Angular! v${VERSION.full}`;
  myMap: any;
  constructor(private elementRef: ElementRef) {}
  goToMerchant(merchantId) {
    //this.navCtrl.push(MerchantPage, { merchantId: merchantId });
    console.log("going to merchant "+merchantId)
  }
  ngAfterViewInit(){
    // setup map
    this.myMap = L.map('mapid').setView([51.505, -0.09], 13);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {}).addTo(this.myMap);
    // note class= merchLink and data-merchId = 200
    var popupLink='<a class="merch-link" data-merchId="200">Show</a>'
    // add marker
    var marker = L.marker([51.5, -0.09])
        .bindPopup(popupLink)
        .addTo(this.myMap);
    let self = this;
    marker.on('popupopen', function() {
      // add event listener to newly added a.merch-link element
      self.elementRef.nativeElement.querySelector(".merch-link")
      .addEventListener('click', (e)=>
      {
        // get id from attribute
        var merchId = e.target.getAttribute("data-merchId");
        self.goToMerchant(merchId)
      });
    });
  }

相关内容

  • 没有找到相关文章

最新更新