如何在Angularjs中使用Switch语句

时间:2016-03-01 13:15:12

标签: javascript angularjs cordova switch-statement

我在我的cordova应用程序中构建了一个app.js.在这个模块中有语音识别,如$ scope.recog。在这个函数中,我想使用 Switch Statement 来获取一个单词,这是我的代码:

$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();
  };

认可是有效的,但是转换声明不起作用我写错了还是我缺少什么?谢谢你的帮助

3 个答案:

答案 0 :(得分:0)

您的开关功能位于您未调用的另一个功能中。如果你想使用那么你需要调用它

答案 1 :(得分: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();

答案 2 :(得分:0)

我建议您使用ui-route在Angular中进行路由。

但是,另一方面,如果你想解决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;