AngularJS - isObject()方法


AngularJS中的isObject()方法主要用於檢查一個引用是否為Object。如果函式中傳遞的引用是物件,此方法將返回True,否則返回False。

注意 - NULL值不被認為是物件,但JavaScript陣列屬於物件。

語法

angular.isObject(value)

示例-檢查引用是否為物件

在你的Angular專案目錄中建立一個檔案“isObject.html”,並複製貼上以下程式碼片段。

<!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>

輸出

要執行以上程式碼,只需進入檔案並像普通HTML檔案一樣執行它。你將在瀏覽器視窗看到以下輸出。

示例2

在你的Angular專案目錄中建立一個檔案“isObject.html”,並複製貼上以下程式碼片段。

<!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>

輸出

要執行以上程式碼,只需進入檔案並像普通HTML檔案一樣執行它。你將在瀏覽器視窗看到以下輸出。

更新於:2021年10月8日

541次瀏覽

開啟你的事業

完成課程取得認證

開始
廣告
© . All rights reserved.