Angular 2和LeafLet Uncaught ReferenceError:<function>未在HTMLButtonElement.onclick中定义

时间:2017-11-19 21:45:38

标签: javascript angular typescript leaflet

我在Leafular的Angular 2中有一个项目,我想通过点击按钮来调用简单的函数,但是我得到了

ReferenceError: <function> is not defined at HTMLButtonElement.onclick

这是我的代码。一切正常,但功能不行。

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

你能帮我解决一下吗?

3 个答案:

答案 0 :(得分:2)

请勿onclick使用HTML作为onclick,并且scope的{​​{1}}全局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"); } } 的一部分。所以你应该尝试的是下面的内容

let popUp = (this.myData[i])['displayName'] +
'<br/><dynamic-button></dynamic-button>';

然后

inline

答案 1 :(得分:2)

您可以尝试使用(click)="OpenList()"

<button (click)="OpenList()" type="button" class="btn-link btn-anchor">List</button>

Typescript功能

OpenList()
 { 
 
 }

答案 2 :(得分:1)

在angular2中你将绑定这样的事件:

let popUp = (this.myData[i])['displayName'] +
    '<br/><button (click)="test">Raumplan</button>';

或者这个:

let popUp = (this.myData[i])['displayName'] +
    '<br/><button on-click="test">Raumplan</button>';