当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。