Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

angular的ng-switch可以用true作为判断条件吗?

如何用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">
<div ng-controller="Ctrl">
    <div 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>
    </div>

    <div>
        <span ng-if="arr.length<2">1---</span>
        <span ng-if="arr.length>=2">2---</span>
    </div>

    <div ng-switch=true>
        <span ng-switch-when="(arr.length==0)"> 1---true </span>
        <span ng-switch-when="(arr.length==0)"> 2---true </span>
    </div>
</div>

<script>
    var App = angular.module("App", []);
    App.controller("Ctrl", ["$scope", function ($scope) {
        $scope.arr = [1, 2,3];
    }]);
</script>
</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<ul ng-switch="arr">
    <li ng-switch-when="1">1111111</li>
    <li ng-switch-when="2">222222222</li>
    <li ng-switch-when="3">333333333</li>
    <li ng-switch-default=" arr>3">4444444444444444444444444444</li>
</ul>

$scope.arr= arr.length;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...