如何在angularJS控制器中编写Switch语句

时间:2015-12-07 10:27:03

标签: angularjs switch-statement

如何在angularJS控制器中编写Switch语句?

我的源代码是

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td><a href="" ng-click="SwitchFuction(x.Name, x.Sno)">{{ x.Country }}</a></td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {

    $scope.names = [
        { Sno: '1', Name: 'Jani', Country: 'Norway' },
        { Sno: '2', Name: 'Hege', Country: 'Sweden' },
        { Sno: '3', Name: 'Kai', Country: 'Denmark' }
    ];

    $scope.SuperFunction = function (id) {
        alert(id);
    };

    $scope.SwitchFuction = function (name, sno) {
        switch (sno) {
            case '1'
                alert("1. Selected Name: " + name );
                break;
            case '2'
                alert("2. Selected Name: " + name );
                break;
            default:

        }
    };

});
</script>

</body>
</html>

如何在函数 SwitchFuction 中编写Switch语句??? 在上面的源代码中包含一些语义错误。请协助如何编写Switch语句?

错误屏幕:

FireFox Screen Shot

3 个答案:

答案 0 :(得分:32)

在每个案例SwitchFunction丢失后,:出现语法错误  正确的代码:

$scope.SwitchFuction = function (id, caseStr) {
        switch (caseStr) {
            case '1':
                alert("Selected Case Number is 1");
                break;
            case '2':
                alert("Selected Case Number is 2");
                break;
            default:

        }
    };

答案 1 :(得分:3)

AngularJS是建立在JavaScript之上的,它与switch case没有不同的语法(只要你在脚本中使用它)。 JavaScript支持使用以下语法切换case语句。

switch (expression) {
  case value1:
    //Statements executed when the result of expression matches value1
    [break;]
  case value2:
    //Statements executed when the result of expression matches value2
    [break;]
  ...
  case valueN:
    //Statements executed when the result of expression matches valueN
    [break;]
  default:
    //Statements executed when none of the values match the value of the expression
    [break;]
}

Reference

答案 2 :(得分:3)

&#13;
&#13;
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="customersCtrl">

    <table>
      <tr ng-repeat="x in names">
        <td>{{ x.Name }}</td>
        <td><a href="" ng-click=SwitchFuction(x.Name,$index)>{{ x.Country }}</a>
        </td>
      </tr>
    </table>

  </div>

  <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope) {

      $scope.names = [{
        Name: 'Jani',
        Country: 'Norway'
      }, {
        Name: 'Hege',
        Country: 'Sweden'
      }, {
        Name: 'Kai',
        Country: 'Denmark'
      }];

      $scope.SuperFunction = function(id) {
        alert(id);
      };

      $scope.SwitchFuction = function(id, caseStr) {
        switch (caseStr) {
          case 0:
            alert("Selected Case Number is 0");
            break;
          case 1:
            alert("Selected Case Number is 1");
            break;
          default:
            alert("Selected Case Number is other than 0 and 1");
            break;

        }
      };

    });
  </script>

</body>

</html>
&#13;
&#13;
&#13;

以下是Plunker