我是打字稿新手。我有一个关于当您单击 html 按钮时如何从 .html 页面调用 .ts 文件内的方法的查询
.ts file
class AdminTS {
public alertMessageTS() {
alert("This is the test call!!!.");
}
}
.html 页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Sample TypeScript page
</title>
<script src="AdminTS.js"></script>
</head>
<body>
<input type="button" value="Click" onclick ="getTrainingName(1)">
</input>
</body>
</html>
我收到运行时异常,提示 getTrainingName 未定义。
getTrainingName()
从未在您的示例代码中定义,也许您忘记添加该部分?
我建议在你的html中根本不要使用javascript,而是使用addEVentListener
向 dom 元素添加处理程序。然后你所有的应用程序逻辑都将在*.ts
files.
(既然你已经使用了 typescript,那么在 html 中添加内联 js 就更没有意义了)
.html 文件
<input type="button" value="Click" id="coolbutton"></input>
.ts file
class AdminTS {
constructor() {
let btn = document.getElementById("coolbutton");
btn.addEventListener("click", (e:Event) => this.getTrainingName(4));
}
getTrainingName(n:number){
// button click handler
}
}
// start the app
new AdminTS();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)