我想使用webkitSpeechRecognition在Angular 7中添加语音识别。 我从演讲中获得了价值,但问题是我无法在vSearch.onresult之外使用它。
我试图在getResult()函数中使用变量“ result”并发送一个get请求,但它说它是未定义的;
这是我在ts中的代码。
voiceSearch() {
let voiceHandler = this.hiddenSearchHandler.nativeElement;
if ("webkitSpeechRecognition" in window) {
const vSearch = new webkitSpeechRecognition();
vSearch.continuous = false;
vSearch.interimresults = false;
vSearch.lang = 'en-US';
vSearch.start();
vSearch.onresult = function(e) {
voiceHandler.value = e.results[0][0].transcript;
this.result = e.results[0][0].transcript;
vSearch.stop();
}
} else {
alert("Your browser does not support voice recognition!");
}
}
getResult() {
console.log(this.result);
}