AngularJS API之equal比較對象

使用狀況

  • 1 首先,全部知足 a === 3 這種的對象,在angular.equals(a,b)中都會返回真
  • 2 全部對象的類型,以及屬性值都相同的,也會返回真
  • 3 NaN和NaN也會返回真(在javascript中,返回的是假)
  • 4 正則也會返回真(在javascirpt,/abc/ /abc/被認爲是不相等的)

樣例

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
</head>
<body ng-app="myApp">
	<div ng-controller="myCtrl">
		{{ a_equals }}
		{{ user_equals }}
		{{ nan_equals }}
		{{ reg_equals }}
	</div>
	<script type="text/javascript">
	angular.module("myApp",[])
	.controller("myCtrl",function($scope){
		
		var a = 3;
		$scope.a_equals = angular.equals(a,3);//a === 3

		var user1 = {"name":"xing","age":30};
		var user2 = {"name":"xing","age":30};
		$scope.user_equals = angular.equals(user1,user2);

		$scope.nan_equals = angular.equals(NaN,NaN);//in javascirpt is false

		$scope.reg_equals = angular.equals(/abc/,/abc/);// in javascript is false
	});
	</script>
</body>
</html>

運行會獲得四個truejavascript

相關文章
相關標籤/搜索