我必须使用 ionic 2、angular 2 和 typescript 构建一个移动应用程序。
我在我的应用程序中使用传单 map 。
我想点击 map 上的一个标记,显示一个包含链接的弹出窗口。
此链接调用 typescript 文件中的函数,但在弹出窗口中不起作用。
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)
}));
}
}
关于javascript - 在 ionic 2 和 angular 2 中添加传单弹出窗口的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43827710/