- 我是 typescript 的新手。
我在 fiddle 中有一个工作原型(prototype),如果我使用它,它不会抛出任何错误。 http://jsfiddle.net/61ufvtpj/2/
但是在 typescript 中我使用了这一行 if(this.checked){ 它抛出了这个错误 [ts] Property 'checked' does not exist on type 'TElement'。
- 你能告诉我如何修复它,以便将来我自己修复它。
- 提供下面的相关代码和要点中的完整代码。
https://gist.github.com/niniyzni/4faea080e53eb0c7155ddd8fb635a46c
$(document).on('change', '#playerFlagCheck', function () {
if(this.checked){ //[ts] Property 'checked' does not exist on type 'TElement'.
$('#editIconplayer').addClass("gridUpdateIcon");
alert("I am inside if");
}else{
alert("I am inside else");
if(!$('#editIconplayer').hasClass("gridUpdateIcon")){
$('#editIconplayer').removeClass("gridUpdateIcon");
}
}
});
最佳答案
您的事件处理程序正在创建一个新上下文,to more about check me .因此,this
不再指代 Controller 本身。
这必须解决问题:
const vm = this;
$(document).on('change', '#playerFlagCheck', function () {
if(vm.checked) {
$('#editIconplayer').addClass("gridUpdateIcon");
alert("I am inside if");
} else {
alert("I am inside else");
if(!$('#editIconplayer').hasClass("gridUpdateIcon")) {
$('#editIconplayer').removeClass("gridUpdateIcon");
}
}
});
关于javascript - 类型 'checked' 上不存在属性 'TElement',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50079457/