提问人:B.Balamanigandan 提问时间:12/7/2015 最后编辑:Prashant PokhriyalB.Balamanigandan 更新时间:10/24/2018 访问量:108732
如何在 angularJS 控制器中编写 Switch 语句
How to write Switch Statement in angularJS Controller
问:
如何在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 语句?
错误屏幕:
答:
3赞
Vivek
12/7/2015
#1
AngularJS 建立在 JavaScript 之上,它的切换语法与 JavaScript 没有不同(只要你在脚本中使用它)。JavaScript 支持使用以下语法切换大小写语句。
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;]
}
评论
0赞
B.Balamanigandan
12/7/2015
还行。那么我上面的脚本有什么问题呢?请协助我的脚本使错误无...
0赞
B.Balamanigandan
12/7/2015
我附上了屏幕截图。请参考它。
0赞
B.Balamanigandan
12/7/2015
如果没有 Switch 语句,它可以正常工作,并且会显示列表,否则它会将屏幕显示为屏幕截图。
0赞
Vivek
12/7/2015
屏幕截图不正确
0赞
B.Balamanigandan
12/7/2015
以上源代码的输出是 {{ x.Name }} {{ x.Country }}
38赞
vpsingh016
12/7/2015
#2
在缺少每个案例后,您中都存在语法错误
正确代码: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:
}
};
3赞
DeviD
2/18/2016
#3
<!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>
这是 Plunker
评论
0赞
DeviD
2/18/2016
以上是所提问题的语法更正代码。这里讨论了 java 脚本切换和参数传递。阅读更多关于$index的信息,请访问:这里
评论