【发布时间】:2021-01-16 12:27:04
【问题描述】:
我有一个获取用户输入的函数。在该函数内部,是一个检查用户输入的回调函数。调用回调函数时,出现错误,ERROR 错误:无法读取未定义的属性“输入”。
HTML
<input id="answer" type="number" (input)= "getInput($event, checkAnswer)" >
JS
input = null;
answer = null;
getInput(event, chkAns){
this.input = Number(event.target.value);
chkAns();
console.log("Input :" + " " + this.input);
}
checkAnswer(){
if(this.input === this.answer){
this.multiply();
} else{
console.log(this.input + " " + "is incorrect" + " " + typeof
this.answer);
}
}
【问题讨论】:
-
可能是因为
this在该上下文中未定义。 -
你能否在你的问题中加入一个有效的 sn-p - 特别是展示如何调用 getInput 以及 chkAns 和 checkAnswer 之间的关系。
-
@AHaworth 链接:app-x.stackblitz.io
标签: javascript angular error-handling undefined