[英]Typescript - Calling function repeatedly from within ForEach() leads to undefined
[英]TypeScript 'this' is undefined when calling a function within HTML String
我想从 HTML 变量中调用一个简单的 TypeScript 函数。 带有 HTML 的 Button 试图调用“_Myfunction”,它创建了一个简单的 Alert();。
单击该按钮会产生以下错误:
typeError: 无法读取未定义的属性“_Myfunction”
我该如何解决这个错误? 代码如下:
constructor() {
super();
}
postInitialize(): void {
}
render() {
this.mapHelper.map.getSelectedAssets().then(function(resolvedValue) {
if(resolvedValue != null){
masterHTMLstring =
<table style="background-color:#FFFFFF;width:200px;">
{resolvedValue.map((item, index) => (
<tr key={index}>
<td>
<button id={item.EntityUid} onclick={() => this._myfunction(item.EntityUid)}>Add</button>
</td>
</tr>
))}
</table>;
}
}, function(error) {
//masterHTMLstring = "error";
});
return <div class="basemap23" >{masterHTMLstring}</div>;
}
destroy(): void {
this._handles.destroy();
this._handles = null;
}
public _myfunction(entityID: string): void {
alert(entityID);
}
替换function(resolvedValue) {
与resolvedValue => {
这应该很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.