AngularJS – angular.isArray() 函数

AngularJS 中的函数基本上检查引用是否为数组。如果函数内部传递的引用是 Array 类型,则此方法将返回 True,否则将返回 False。angular.isArray()

语法

angular.isArray(value)

示例 - 检查引用是否为数组

在您的 Angular 项目目录中创建一个文件“ isArray.html ”并复制粘贴以下代码片段。

<!DOCTYPE html>
<html>
   <head>
      <title>angular.isArray()</title>
      <script xx_src="https://cdn.staticfile.org/angularjs/1.3.2/angular.min.js"></script>
   </head>
   <body ng-app="app" style="text-align:center">
      <h1 style="color:green">
         Welcome to nhooo.com
      </h1>
      <h2>AngularJS | angular.isArray()</h2>
      <div ng-controller="example">
         <b>Value: {{value}}</b>
         <br><br>
         {{isArray}}
         <br><br>
         <b>Value: {{value2}}</b>
         <br><br>
         {{isArray1}}
         <br><br>
         <b>Value: {{value3}}</b>
         <br><br>
         {{isArray2}}
         <br><br>
         <b>Value: {{value4}}</b>
         <br><br>
         {{isArray3}}
      </div>
      <!-- Script for passing the values and checking... -->
      <script>
         var app = angular.module("app", []);
         app.controller('example',['$scope', function ($scope)
         {
         // Defining the keys & values
         $scope.value = [];
         $scope.value2 = null;
         $scope.value3 = [{id:'1'},{id:'2'},{id:'3'},{id:'4'},{id:'5'}];
         $scope.value4 = [1,2,3,4,5];


         $scope.isArray = angular.isArray($scope.value) == true
            ? "$scope.array is an Array."
            : "$scope.array is not an Array.";

         $scope.isArray1 = angular.isArray($scope.value2) == true
            ? "$scope.array is an Array."
            : "$scope.array is not an Array.";

         $scope.isArray2 = angular.isArray($scope.value3) == true
            ? "$scope.array is an Array."
            : "$scope.array is not an Array.";

         $scope.isArray3 = angular.isArray($scope.value4) == true
            ? "$scope.array is an Array."
            : "$scope.array is not an Array.";
        }]);
      </script>
   </body>
</html>
输出结果

要运行上述代码,只需转到您的文件并将其作为普通 HTML 文件运行即可。您将在浏览器窗口中看到以下输出。