Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 检测Angular:indexOf()中的索引无法正常工作,因为$$hasKeys?_Angularjs_Hash_Key_Indexof - Fatal编程技术网

Angularjs 检测Angular:indexOf()中的索引无法正常工作,因为$$hasKeys?

Angularjs 检测Angular:indexOf()中的索引无法正常工作,因为$$hasKeys?,angularjs,hash,key,indexof,Angularjs,Hash,Key,Indexof,以下情况: role: { roleid=3, name="admin"} availableRoles: [ { roleid=3, name="admin", $$hashKey="object:222"}, { roleid=4, name="plain user", $$hashKey="object:223"} ] currentRoles: [ { roleid=3, name="admin"

以下情况:

role: { roleid=3,  name="admin"}

availableRoles:
    [
        { roleid=3,  name="admin",  $$hashKey="object:222"}, 
        { roleid=4,  name="plain user",  $$hashKey="object:223"}
    ]

currentRoles: 
    [
        { roleid=3,  name="admin"}
    ]
以下尝试:

currentRoles.indexOf(role);  // works properly and outputs 0
availableRoles.indexOf(role);  // does not work 
我可以想象,这是因为$$Haskey。但我没有把它们放在那里,AngularJS确实增加了这些数据

我怎样才能克服这种情况

此数据结构中是否有类似于:忽略角度键的函数?

编辑:

角度对象比较:

因此,您可以只编写函数:

function arrayObjectIndexOf(arr, obj){
    for(var i = 0; i < arr.length; i++){
        if(angular.equals(arr[i], obj)){
            return i;
        }
    };
    return -1;
}

因此它与hashKey无关。要与对象进行比较(比如,在数组中查找索引),您必须创建一个比较循环,或者重载Object的“==”操作符来比较值而不是指针,我认为JS中不允许这样做。

  • 最好的办法是不要有这样的东西

  • 可以使用角度过滤器:

  • 您可以使用其他一些js库(lodash、下划线等)来实现这些功能

您可以借助于角形foreach进行调整。你不能直接检查它,因为indexof works直接数组不包含对象。我们在角度范围内,应该有角度解吗?
var role = { roleid:3,  name:"admin"};

var availableRoles =
    [
        { roleid:3,  name:"admin"}, 
        { roleid:4,  name:"plain user",  $$hashKey:"object:223"}
    ];
alert(availableRoles.indexOf(role));
function contains(arr, id) {
    return $filter('filter')(arr, {roleid : id}, true).length != 0;
}