2016-03-01 82 views
-1

我在我的cordova應用程序中構建了一個app.js。在這個模塊中有語音識別,如$ scope.recog。在這個功能我要趕使用switch語句一句話,這是我的代碼:如何在Angularjs中使用Switch語句

$scope.recog = function() { 
    var recognition = new SpeechRecognition(); 
    recognition.onresult = function(event) { 
    var result = event.results[0][0].transcript; 
    $scope.SwitchFuction = function (result){ 
     switch(result){ 
     case 'login': 
     $scope.loginFn(); 
      break; 
     case 'sign up': 
     $location.path('/register'); 
      break; 
     case 'register': 
     $scope.registerFn(); 
      break; 
     case 'cancel': 
     $scope.cancelregisterFn(); 
      break; 
     case 'go to home': 
     $location.path('/home'); 
      break; 
     case 'go to add friend': 
     $location.path('/addfriend'); 
      break; 
     case 'go to friend request': 
     $location.path('/friendrequest'); 
      break; 
     case 'go to pending request': 
     $location.path('/penddingrequest'); 
      break; 
     case 'add': 
     $scope.addfriends(); 
      break; 
     case 'log out': 
     $scope.logout(); 
      break; 
     } 
    }; 
    $scope.$apply() 
    }; 
    recognition.start(); 
    }; 

的識別工作,但switch語句不工作我錯寫它還是有我錯過了什麼?感謝您的幫助

+0

你在哪裏調用'$ scope.SwitchFunction(結果);'? – jnthnjns

+0

它的我的壞,對不起,我不應該使用該功能,謝謝 –

+0

我建議你使用'ui路由'而不是開關案件。您可以閱讀有關'ui-route'的文檔:https://github.com/angular-ui/ui-router –

回答

0

您的開關功能位於您未調用的另一個功能中。如果你想使用,那麼你需要調用它

+0

或者刪除'SwitchFunction()'並以程序方式運行開關盒。 – jnthnjns

+0

多數民衆贊成我的不好,當我仰望這個問題http://stackoverflow.com/questions/34131341/how-to-write-switch-statement-in-angularjs-controller我雖然我也必須使該功能,謝謝你你的幫助,我將刪除該功能 –

0

你沒有正確調用SwitchFunction。這可能會幫助你。

$scope.SwitchFuction = function (result){ 
    switch(result){ 
    case 'login': 
     $scope.loginFn(); 
     break; 
    case 'sign up': 
     $location.path('/register'); 
     break; 
    case 'register': 
     $scope.registerFn(); 
     break; 
    case 'cancel': 
     $scope.cancelregisterFn(); 
     break; 
    case 'go to home': 
     $location.path('/home'); 
     break; 
    case 'go to add friend': 
     $location.path('/addfriend'); 
     break; 
    case 'go to friend request': 
     $location.path('/friendrequest'); 
     break; 
    case 'go to pending request': 
     $location.path('/penddingrequest'); 
     break; 
    case 'add': 
     $scope.addfriends(); 
     break; 
    case 'log out': 
     $scope.logout(); 
     break; 
} 
$scope.recog = function() { 
    var recognition = new SpeechRecognition(); 
    recognition.onresult = function(event) { 
    var result = event.results[0][0].transcript; 
    $scope.SwitchFuction(result); 
    $scope.$apply(); 
}; 

recognition.start(); 
+0

'SwitchFunction'被「正確」定義。 – jnthnjns

+0

但是,沒有正確地被調用。 –

+1

正確,已經建立。請更新您的OP和任何未來用戶的答案。 – jnthnjns

0

我建議你在Angular中使用ui-route進行路由。

但是,另一方面,如果你想解決switch-case的錯誤試試這個:

function switchFunction (result) { 
     switch(result){ 
     case 'login': 
      $scope.loginFn(); 
      break; 
     case 'sign up': 
      $location.path('/register'); 
      break; 
     case 'register': 
      $scope.registerFn(); 
      break; 
     case 'cancel': 
      $scope.cancelregisterFn(); 
      break; 
     case 'go to home': 
      $location.path('/home'); 
      break; 
     case 'go to add friend': 
      $location.path('/addfriend'); 
      break; 
     case 'go to friend request': 
      $location.path('/friendrequest'); 
      break; 
     case 'go to pending request': 
      $location.path('/penddingrequest'); 
      break; 
     case 'add': 
      $scope.addfriends(); 
      break; 
     case 'log out': 
      $scope.logout(); 
      break; 
     } 
    } 

    function onResult (event) { 
     var result = event.results[0][0].transcript; 
     switchFunction(result); //YOU FORGET THIS CALL 
    } 

    function recog() { 
     var recognition = new SpeechRecognition(); 
     recognition.onresult = onResult; 
     recognition.start(); 
     $scope.$apply() 
    } 

    $scope.recog = recog; 
    $scope.switchFunction = switchFunction;