AngularJS – isObject() method


The isObject() method in AngularJS basically checks if a reference is an Object or not. This method will return True if the reference passed inside the function is an Object, else it will return False.

Note − NULL values are not considered as an Object, but JavaScript arrays are objects.

Syntax

angular.isObject(value)

Example − Check if the reference is an Object or not

Create a file "isObject.html" in your Angular project directory and copy-paste the following code snippet.

<!DOCTYPE html>
<html>
   <head>
      <title>angular.isObject()</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.isObject()</h2>

      <div ng-controller="example">
      <b>Value: {{value}}</b>
      <br><br>
      {{isObject}}
      <br><br>
      <b>Value: null</b>
      <br><br>
      {{isObject1}}
      <br><br>
      <b>Value: {{value3}}</b>
      <br><br>
      {{isObject2}}
      <br><br>
      <b>Value: {{value4}}</b>
      <br><br>
      {{isObject3}}

   </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 = {name: "Tutorials Point"};
         $scope.value2 = null;
         $scope.value3 = [{id:'1'},{id:'2'},{id:'3'},{id:'4'},{id:'5'}];
         $scope.value4 = "Tutorials Point";

         $scope.isObject = angular.isObject($scope.value) == true
            ? "$scope.object is an object."
            : "$scope.object is not an object.";

         $scope.isObject1 = angular.isObject($scope.value2) == true
            ? "$scope.object is an object."
            : "$scope.object is not an object.";

         $scope.isObject2 = angular.isObject($scope.value3) == true
            ? "$scope.object is an object."
            : "$scope.object is not an object.";

         $scope.isObject3 = angular.isObject($scope.value4) == true
            ? "$scope.object is an object."
            : "$scope.object is not an object.";
         }]);
      </script>
   </body>
</html>

Output

To run the above code, just go to your file and run it as a normal HTML file. You will see the following output on the browser window.

Example 2

Create a file "isObject.html" in your Angular project directory and copy-paste the following code snippet.

<!DOCTYPE html>
<html>
   <head>
      <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
      </script>
      <title>
         AngularJS | angular.isObject()
      </title>
   </head>

<body ng-app="app"
   style="text-align:center">
   <h1 style="color:green">
   Welcome to Tutorials Point
</h1>
   <h2>
   angular.isObject()
</h2>

   <body ng-app="app">
      <div ng-controller="app">
         <b>Input1: </b>
      <span>
         <code>object = {"Name": "SIMPLY LEARNING"}</code>
      </span>
         <br>
         <b>Is Object? : </b>{{isObject1}}
         <br><br><br>
         <b>Input2: </b>
         <code>null</code>
         <b>, Is Object? : </b> {{isObject2}}
         <br><br><br>
         <b>Input3: </b>
         <code>{}</code>
         <b>, Is Object? : </b> {{isObject3}}
         <br><br><br>
         <b>Input4: </b>
         <code>""</code>
         <b>, Is Object? : </b> {{isObject4}}
         <br><br><br>
      </div>
      <script>
         var app = angular.module("app", []);
         app.controller('app', ['$scope', function($scope) {
            // Defining the objects
            var obj1 = {
               "Name": "SIMPLY LEARNING"
            };

            var obj2 = null;
            var obj3 = {};
            var obj4 = "";
            $scope.isObject1 = angular.isObject(obj1);
            $scope.isObject2 = angular.isObject(obj2);
            $scope.isObject3 = angular.isObject(obj3);
            $scope.isObject4 = angular.isObject(obj4);
         }]);
      </script>
   </body>
</html>

Output

To run the above code, just go to your file and run it as a normal HTML file. You will see the following output on the browser window.

Updated on: 08-Oct-2021

406 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements