I want to add a speech recognition in Angular 7 using the webkitSpeechRecognition. I get the value from the speech but the problem is that I can't use it outside of vSearch.onresult.
I tried to use the variable "result" in the getResult() function and send a get request but it says it's undefined;
Here is my code in the 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);
}