Javascript 无法读取属性';输入';未定义的
我有一个函数,可以获取用户的输入。在该函数中,有一个检查用户输入的回调函数。调用回调函数时,出现错误,错误:无法读取未定义的属性“input” HTMLJavascript 无法读取属性';输入';未定义的,javascript,angular,error-handling,undefined,Javascript,Angular,Error Handling,Undefined,我有一个函数,可以获取用户的输入。在该函数中,有一个检查用户输入的回调函数。调用回调函数时,出现错误,错误:无法读取未定义的属性“input” HTML 您可以执行以下三项操作之一: 使你传递的函数(chkAns)原子化——也就是说,它不需要特定的状态(this.input,this.answer),并在调用时获取这些参数(因此,你将执行chkAns(this.input,this.answer) 不要从模板传递它,而是调用this.CheckAnswer()insidegetInput 调用时
您可以执行以下三项操作之一:
chkAns(this.input,this.answer)
this.CheckAnswer()
insidegetInput
this
contex传递给chkAns:chkAns.call(this)
-我认为这个选项是最不“安格化”的选项这是第二个选项的工作,可能是因为该选项在该上下文中未定义。您能否在问题中插入一个工作片段,特别是显示如何调用getInput以及chkan和checkAnswer之间的关系。@AHaworth链接:
<input id="answer" type="number" (input)= "getInput($event, checkAnswer)" >
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);
}
}