如何用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>
雷雷