如何用ng-switchh实现根据数组长度不同,显示不同的内容,假如用arr.length
作为判断条件的话需要写很多情况,所以考虑用true作为判断条件,但是不知道ng-switch-when
该怎么写,当然用ng-if
也可以实现,只是对ng-switch
的写法有疑问,谢谢。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<script src="angular.js"></script>
<style>
</style>
</head>
<body ng-app="App">
<p ng-controller="Ctrl">
<p ng-switch="arr.length">
<span ng-switch-when="1">1</span>
<span ng-switch-when="2">2</span>
<span ng-switch-when="3">3</span>
</p>
<p>
<span ng-if="arr.length<2">1---</span>
<span ng-if="arr.length>=2">2---</span>
</p>
<p ng-switch=true>
<span ng-switch-when="(arr.length==0)"> 1---true </span>
<span ng-switch-when="(arr.length==0)"> 2---true </span>
</p>
</p>
<script>
var App = angular.module("App", []);
App.controller("Ctrl", ["$scope", function ($scope) {
$scope.arr = [1, 2,3];
}]);
</script>
</body>
</html>