Angular.js – using angular.isArray() function

A simple Angular.js example of checking if a variable is an array.
<!DOCTYPE html>
<html ng-app="testApp">
<head>

<style>

</style>

    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen Angular.js example"/>

    <title>Tizen Angular.js example</title>

    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>

    <script src="js/jquery-2.1.4.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/angular.min.js"></script>
    <script>

        var app = angular.module('testApp', []);

        app.controller('AppController', function($scope) {

          var sampleObj = {};
              sampleObj.weight = 60;
              sampleObj.height = 180;
              sampleObj.colors = {};
              sampleObj.colors.r = 100;
              sampleObj.colors.g = 178;
              sampleObj.colors.b = 245;

              $scope.obj1 = sampleObj;
              $scope.obj2 = ["A","B","C","D","E"];

            $scope.check = function() {

              $scope.check1 = angular.isArray($scope.obj1);
              $scope.check2 = angular.isArray($scope.obj2);

            }

        });

    </script>
</head>
<body>
  <div ng-controller="AppController">
  <br>
  var sampleObj = {};<br>
      sampleObj.weight = 60;<br>
      sampleObj.height = 180;<br>
      sampleObj.colors = {};<br>
      sampleObj.colors.r = 100;<br>
      sampleObj.colors.g = 178;<br>
      sampleObj.colors.b = 245;
  <br><br>
      $scope.obj1 = sampleObj;<br>
      $scope.obj2 = ["A","B","C","D","E"];

  <br><br>
    <button ng-mousedown="check()">
      Click this button to check if objects are arrays
    </button>
    <br><br>
    Is the <b>$scope.obj1</b> an array: <b> {{ check1 }} </b>
    <br><br>
    Is the <b>$scope.obj2</b> an array: <b> {{ check2 }} </b>
  </div>
</body>

</html>

Responses

0 Replies