如何在完成说话识别后执行(if)语句

时间:2015-04-24 14:33:52

标签: javascript angularjs ionic-framework cordova-plugins

它在变量comp仍然为空时进行比较。它之前应该采用语音识别器的值。我该如何解决这个问题?我怎么能让它等到语音识别器完成它的工作?

.controller('LearnCtrl', function($scope , $ionicSlideBoxDelegate, Slides ) {

$scope.slides = Slides.all();

$scope.hear = function() {
    navigator.tts.startup();
    navigator.tts.setLanguage('fr');

    navigator.tts.speak($scope.slides[$ionicSlideBoxDelegate.currentIndex()].name);
 }

$scope.spell = function() {
    var maxMatches = 1;
    var promptString = "Speak now";
    var language ='fr-FR'
    var comp ="";

    window.plugins.speechrecognizer.startRecognize(function(result) {
        comp=result ; 
    }, function(error) {
        alert("connexion lost"+error);
    }, maxMatches, promptString, language);


    if (comp==$scope.slides[$ionicSlideBoxDelegate.currentIndex()].name){
        alert('good job')
    } else {
        alert('try again');
    }

}

1 个答案:

答案 0 :(得分:0)

很可能是异步的,所以请尝试将比较放在你传递给startRecognize的函数中。

window.plugins.speechrecognizer.startRecognize(function(result) {
    comp=result;
    if (comp==$scope.slides[$ionicSlideBoxDelegate.currentIndex()].name){
      alert('good job')
    } else {
      alert('try again');
    }
}, function(error) {
    alert("connexion lost"+error);
}, maxMatches, promptString, language);