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文件一样运行它。你将在浏览器窗口看到以下输出。
广告