如何在angularJS控制器中编写Switch语句 [英] How to write Switch Statement in angularJS Controller

查看:98
本文介绍了如何在angularJS控制器中编写Switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to write Switch Statement in angularJS Controller ?

我的源代码是

<!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语句?

How to Write the Switch Statement within the function SwitchFuction ??? In the above Source Code contains some semantic error. Kindly assist how to write the Switch Statement?

错误屏幕:

推荐答案

在每种情况下丢失:后,SwitchFunction中都存在语法错误 正确的代码:

there is a syntax error in your SwitchFunction after each case : is missing correct code:

$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:

        }
    };

这篇关于如何在angularJS控制器中编写Switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆