1

我想使用 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);
}
4

0 回答 0