简体   繁体   English

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

[英]Angular 2 and LeafLet Uncaught ReferenceError: <function> is not defined at HTMLButtonElement.onclick

I have a project in Angular 2 with Leaflet, I would like to call the simple function by clicking the button, but I get我在 Angular 2 中有一个带有 Leaflet 的项目,我想通过单击按钮来调用简单的函数,但是我得到了

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

Here is my code.这是我的代码。 Everything works, but the function not.一切正常,但功能不行。

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

Could you please help me with workaround?你能帮我解决方法吗?

Do not use onclick as it's an HTML and the function you assign to onclick will be searched in global scope of javascript ie inside <script> tags and this is no more the part of Angular2 .不要使用onclick因为它是一个HTML并且您分配给onclick的函数将在javascript全局scope搜索,即在<script>标签内,这不再是Angular2的一部分。 So what you should try is something like below所以你应该尝试像下面这样

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

And then接着

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

in angular2 you will bind the event like this:在 angular2 中,您将像这样绑定事件:

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

OR this:或者这个:

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

You can try using (click)="OpenList()"您可以尝试使用(click)="OpenList()"

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

Typescript function Typescript功能

OpenList()
 { 
 
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的ReferenceError :(函数)未在HTMLButtonElement.onclick中定义 - Uncaught ReferenceError: (function) not defined at HTMLButtonElement.onclick Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick - Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义函数 - Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick 得到此错误Uncaught ReferenceError:函数未在HTMLButtonElement.Onclick上定义? - Getting this error Uncaught ReferenceError: function is not defined at HTMLButtonElement.Onclick? ES6模块“Uncaught ReferenceError:函数未在HTMLButtonElement.onclick中定义” - ES6 module “Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick” 未捕获的ReferenceError:未在HTMLButtonElement.onclick中定义查找 - Uncaught ReferenceError: lookup is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:showCurrentPage 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: showCurrentPage is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:在 HTMLButtonElement.onclick 中未定义 postAcmgForm - Uncaught ReferenceError: postAcmgForm is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:HTMLButtonElement.onclick中未定义submitToAPI - Uncaught ReferenceError: submitToAPI is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:id 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: id is not defined at HTMLButtonElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM