Ann*_*a F 5 javascript leaflet typescript angular
我在 Angular 2 中有一个带有 Leaflet 的项目,我想通过单击按钮来调用简单的函数,但是我得到了
ReferenceError: <function> is not defined at HTMLButtonElement.onclick
Run Code Online (Sandbox Code Playgroud)
这是我的代码。一切正常,但功能不行。
export class MapComponent implements OnInit {
//layer
googleStreets = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',{
maxZoom: 30,
subdomains:['mt0','mt1','mt2','mt3']
});
//!!!The FUNCTION
test() {
console.log('test');
}
constructor () {}
ngOnInit() {
this.ParseService.getJ().subscribe(data => {
this.myData = JSON.parse(data);
//set markers and popUps
for(let i = 0; i < this.myData.length; i++) {
let lat =+((this.myData[i])['lat']);
let lng =+((this.myData[i])['lng']);
this.id = (this.myData[i])['code'];
//HERE IS THE PROBLEM. BUTTON APPEARS BUT FUNCTION IS NOT AVAILABLE
let popUp = (this.myData[i])['displayName'] +
'<br/><button onclick="test">Raumplan</button>';
let markerLocation = L.latLng(lat, lng);
let marker = new L.Marker(markerLocation, {icon: customIcon});
map.addLayer(marker);
marker.bindPopup(popUp);
}
});
Run Code Online (Sandbox Code Playgroud)
你能帮我解决方法吗?
不要使用,onclick因为它是一个HTML,您分配给的函数onclick将在ie 内部标签的全局scope中搜索,这不再是. 所以你应该尝试像下面这样javascript<script>Angular2
import {Component} from '@angular/core';
@Component({
selector: 'dynamic-button',
template: '<button type="button" (click)="test()">Raumplan</button>'
})
export class DynamicButton{
public test(): void{
alert("Hi. I am a test call");
}
}
Run Code Online (Sandbox Code Playgroud)
进而
let popUp = (this.myData[i])['displayName'] +
'<br/><dynamic-button></dynamic-button>';
Run Code Online (Sandbox Code Playgroud)
在 angular2 中,您将像这样绑定事件:
let popUp = (this.myData[i])['displayName'] +
'<br/><button (click)="test">Raumplan</button>';
Run Code Online (Sandbox Code Playgroud)
或这个:
let popUp = (this.myData[i])['displayName'] +
'<br/><button on-click="test">Raumplan</button>';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5209 次 |
| 最近记录: |