我试图使用angularjs与对象数组进行比较。我尝试过使用angular.equals而没有运气。我甚至将它们分配给变量进行比较(最初它们不是),但它仍然没有检测到它们。我想看看数组是否匹配。如果他们这样做 - 在{{equals}}
中提醒和/或反映真实
<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
function MyCtrl($scope) {
$scope.thePath = {};
$scope.thePath2 = {};
$scope.clickme = function(val) {
if (val in $scope.thePath) {
alert("already present");
console.log($scope.thePath);
return
}
$scope.thePath[val] = 1;
}
$scope.clickme2 = function(val) {
if (val in $scope.thePath2) {
alert("already present");
console.log($scope.thePath2);
return
}
$scope.thePath2[val] = 1;
}
var obj1 = $scope.thePath;
var obj2 = $scope.thePath2;
//$scope.thepath vs $scope.thepath2
$scope.thePath = obj1;
$scope.thePath2 = obj2;
$scope.equals = angular.equals(obj1, obj2);
$scope.doubleCheck = function() {
alert(obj1, obj2);
}
}
</script>
</head>
<body>
<div ng-app ng-controller="MyCtrl">
<p>Section A</p>
<button id="uno" ng-click="clickme('Step A')">Path A</button><br>
<button id="dos" ng-click="clickme('Step B')">Path B</button><br>
<button is="cuatro" ng-click="clickme('Step C')">Path C</button><br>
<button id="cinco" ng-click="clickme('Step D')">Path D</button><br>
<button id="sieta" ng-click="clickme('Step E')">Path E</button> {{thePath}}
<p>Section B</p>
<button id="uno" ng-click="clickme2('Step A')">Path A</button><br>
<button id="dos" ng-click="clickme2('Step B')">Path B</button><br>
<button is="cuatro" ng-click="clickme2('Step C')">Path C</button><br>
<button id="cinco" ng-click="clickme2('Step D')">Path D</button><br>
<button id="sieta" ng-click="clickme2('Step E')">Path E</button> {{thePath2}}
</div>
<br><br>
<p>Do these match?</p><button ng-click="doubleCheck()">Check</button> {{equals}}
</body>
</html>
答案 0 :(得分:0)
angular.equals()
不会比较对象中键的顺序。你能做的是JSON.stringify(obj1) === JSON.stringify(obj2)