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

how can i execute the( if ) statement after finishing the speak recognition

比较变量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');
    }

}

它很可能是异步的,因此请尝试将比较放在传递给 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);