AngularJS – angular.isArray() 函式


AngularJS 中的 angular.isArray() 函式主要用於檢查一個引用是否是陣列。如果函式中傳入的引用是陣列型別,此方法將返回 True,否則將返回 False。

語法

angular.isArray(value)

示例 − 檢查引用是否是陣列

在您的 Angular 專案目錄中建立一個 “isArray.html” 檔案,然後貼上以下程式碼片段。

<!DOCTYPE html>
<html>
   <head>
      <title>angular.isArray()</title>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
   </head>
   <body ng-app="app" style="text-align:center">
      <h1 style="color:green">
         Welcome to Tutorials Point
      </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 檔案執行即可。您將在瀏覽器視窗中看到以下輸出。

更新日期: 06-10-2021

1K+ 次瀏覽

開啟您的 事業

修完課程並獲取認證

開始學習
廣告
© . All rights reserved.