當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


AngularJS angular.isArray()用法及代碼示例


AngularJS中的angular.isArray()函數用於在引用為數組的情況下返回TRUE,在引用不是數組的情況下返回FALSE。句法:

angular.isArray(value)

返回值:如果該值是一個數組,則返回TRUE,否則將返回FALSE。

範例1:


<html> 
    <head> 
        <title>angular.isArray()</title> 
    <script src= 
    "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js">  
    </script> 
    </head> 
    <body ng-app="app" style="text-align:Center"> 
        <h1 style="color:green">GeeksforGeeks</h1> 
  
        <h2>angular.isArray()</h2> 
  
        <div ng-controller="geek"> 
            <b>Sorting Algos:</b> 
            <div ng-repeat="i in sort">{{i.name}}</div> 
            <br><br> 
            isArray:{{isArray}} 
        </div> 
  
        <script> 
        var app = angular.module("app", []); 
        app.controller('geek', ['$scope', function ($scope) { 
            $scope.sort = []; 
            var values = [ 
            { name:'Merge sort' },  
            { name:'Quick sort' },  
            { name:'Bubble sort' } 
            ]; 
            if (angular.isArray(values)) { 
                $scope.isArray = true; 
                angular.forEach(values, function (value, key) { 
                    $scope.sort.push(value) 
                }) 
            } 
        }]); 
        </script> 
    </body> 
</html>

輸出:
isarray

範例2:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>angular.isArray()</title> 
    <script src= 
    "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js">  
    </script> 
    </head> 
    <body ng-app="app" style="text-align:Center"> 
        <h1 style="color:green">GeeksforGeeks</h1> 
        <h2>angular.isArray()</h2> 
        <div ng-controller="geek"> 
        <b>Input:</b>{{name}} 
        <br><br> 
            <b>isArray:</b> {{isArray}} 
        </div> 
        <script> 
            var app = angular.module("app", []); 
            app.controller('geek', ['$scope', function ($scope) { 
                var values = 'GeeksforGeeks'; 
                $scope.name = values; 
                $scope.isArray = angular.isArray(values) 
            }]); 
        </script> 
    </body> 
</html>

輸出:
isarray



相關用法


注:本文由純淨天空篩選整理自Vishal Chaudhary 2大神的英文原創作品 AngularJS | angular.isArray() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。