search

Home  >  Q&A  >  body text

angular.js - Can angular's ng-switch use true as a judgment condition?

How to use ng-switchh to display different contents according to different array lengths. If you use arr.length as the judgment condition, you need to write a lot of situations, so I considered using true as the judgment condition, but I don’t know how to write ng-switch-when , of course it can also be achieved using ng-if, but I have a question about how to write ng-switch, thank you.
The code is as follows:

<!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>
漂亮男人漂亮男人2810 days ago740

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-15 17:15:13

    <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;

    reply
    0
  • Cancelreply