我怎样才能找出为什么两个对象不比较angular.equals平等的吗?平等、对象、两个、angular

2023-09-13 03:39:30 作者:时间煮雨我煮鱼

我用下面的 AngularJS code

if (angular.equals(rowData, $scope.grid.backup[i])) {
   console.log('equal')
}

请注意,AngularJS具有等于函数,每个元件的对象物的内部进行比较。

Please note that AngularJS has an equals function that compares each element inside the object.

这两个物体看起来平等的,当我调试,但角度不同意。我看不出有什么不公平。是否有一些其他的方式,我可以做一个比较?

The two objects look equal when I debug but Angular does not agree. I just cannot see what is not equal. Is there some other way I can do a comparison?

推荐答案

angular.equals的文档()说:

两个对象或值被认为是等价的,如果的至少一个  以下是真实的:

Two objects or values are considered equivalent if at least one of the following is true:

这两个对象或值传递===比较。

Both objects or values pass === comparison.

这两个对象或值是相同类型,并且所有的属性是通过将其与angular.equals比较相等。

Both objects or values are of the same type and all of their properties are equal by comparing them with angular.equals.

这两个值都为NaN。 (在JavaScript中,NaN的==> NAN =>假的。但我们考虑两个NaN的平等)

Both values are NaN. (In JavaScript, NaN ==> NaN => false. But we consider two NaN as equal)

这两个值重新present同一个正前pression(在使用JavasScript,/ ABC / == / ABC / =>假的。但我们考虑两种常规的前pressions作为平等时,他们的文字重presentation匹配)。

Both values represent the same regular expression (In JavasScript, /abc/ == /abc/ => false. But we consider two regular expressions as equal when their textual representation matches).

所以,如果你得到一个错误的 .equals(),那么我们可以得出以下结论:

So, if you're getting a false for .equals(), then we can conclude the following:

这两个是不一样的对象,所以他们不通过 === 这两个值不是NAN(你是presumably工作对象反正)这两个值是不一样的正则表达式

所以,只留下第2项,这意味着无论对象是不相同的类型或它们的一些性质是不相同的文件中。任何人都可以帮助您进一步的究竟是他们之间的不同,我们不得不看到实际的对象或创建它们的code。

So, that only leaves the 2nd item in the documentation which means that either the objects are not the same type or some of their properties are not the same. For anyone to help you further on what exactly is different between them, we'd have to see the actual objects or the code that creates them.

如果您已经安装在你的页面的角非最小化版本,你也可以通过你们的呼叫一步 angular.equals(),看看在哪一步在code这是找到差异。

If you have the non-minimized version of angular installed in your page, you could also just step through your call to angular.equals() and see which step in the code it is finding the difference.

或者,如果有很多属性或大量物体在调试器步进如此的困难,你可以写你自己的小调试程序来告诉你哪些财产是不同的。这将是这个样子:

Or, if there are a lot of properties or a lot of objects so stepping in the debugger is difficult, you could write your own little debug routine to tell you which property was different. That would look something like this:

function compareObjects(s, t) {
    if (typeof s !== typeof t) {
        console.log("two objects not the same type");
        return;
    }
    if (typeof s !== "object") {
        console.log('arguments are not typeof === "object"');
        return;
    }
    for (var prop in s) {
        if (s.hasOwnProperty(prop)) {
            if (t.hasOwnProperty(prop)) {
                if (!angular.equals(s[prop], t[prop])) {
                    console.log("property " + prop + " does not match");
                }
            } else {
                console.log("second object does not have property " + prop);
            }
        }
    }
    // now verify that t doesn't have any properties 
    // that are missing from s
    for (prop in t) {
        if (t.hasOwnProperty(prop)) {
            if (!s.hasOwnProperty(prop)) {
                console.log("first object does not have property " + prop);
            }
        }
    }
}


// then call this function on your two objects
// and then look in the debug console to see what it reports
compareObjects(rowData, $scope.grid.backup[i]);