AngularJS 中的isObject()方法主要检查引用是否为对象。如果函数内部传递的引用是一个对象,则此方法将返回 True,否则将返回 False。
注意- NULL 值不被视为对象,但 JavaScript 数组是对象。
angular.isObject(value)
在您的 Angular 项目目录中创建一个文件“ isObject.html ”并复制粘贴以下代码片段。
<!DOCTYPE html> <html> <head> <title>angular.isObject()</title> <script xx_src="https://cdn.staticfile.org/angularjs/1.3.2/angular.min.js"> </script> </head> <body ng-app="app" style="text-align:center"> <h1 style="color:green"> Welcome to nhooo.com </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: "nhooo.com"}; $scope.value2 = null; $scope.value3 = [{id:'1'},{id:'2'},{id:'3'},{id:'4'},{id:'5'}]; $scope.value4 = "nhooo.com"; $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 文件运行即可。您将在浏览器窗口中看到以下输出。
在您的 Angular 项目目录中创建一个文件“ isObject.html ”并复制粘贴以下代码片段。
<!DOCTYPE html> <html> <head> <script xx_src= "https://cdn.staticfile.org/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 nhooo.com </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) { //定义对象 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 文件运行即可。您将在浏览器窗口中看到以下输出。