简体   繁体   English

AngularJS / JavaScript从Localstorage数组中删除特定元素

[英]AngularJS/JavaScript Remove specific element from Localstorage array

I have an array in my LocalStorage that I manipulate with ngStorage. 我的LocalStorage中有一个数组,可以使用ngStorage进行操作。 I am trying to remove items from there but when I use 我正在尝试从那里删除物品,但是当我使用

$localStorage.someArrayName.splice(id, 1);

only the first removed item works fine, then removing stop working as I would like to. 只有第一个删除的项目可以正常工作,然后按照我的意愿删除停止工作。

I also tried something like this: 我也尝试过这样的事情:

        if (id === $localStorage.someArrayName.length) {
            $localStorage.someArrayName.pop();
        }

        else if (id === 0) {
            $localStorage.someArrayName.shift();
        }

        else {
            $localStorage.someArrayName.splice(id, 1);
        }

But I get even more buggy result 但是我得到了更多的越野车结果

I tried from the example in https://github.com/gsklee/ngStorage like this: 我尝试从https://github.com/gsklee/ngStorage中的示例尝试如下:

delete $localStorage.someArrayName[id];

but it deletes the values and I get null,null,null values in my local storage array. 但是它将删除这些值,并且我在本地存储阵列中得到null,null,null值。 And then I even get ng repeat error about duplicate values: 然后我甚至收到有关重复值的ng重复错误:

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed... 错误:[ngRepeat:dupes]不允许在转发器中重复...

I cannot handle it. 我处理不了 I am not sure if it's some small issue or I am doing something fundamentally wrong. 我不确定这是一个小问题还是我做的是根本错误的事情。 Can you give me some guidance? 你能给我一些指导吗? Thank you in advance! 先感谢您!

This is how my array looks like saved in the localstorage: 这是我的数组保存在localstorage中的样子:

[{"content":"something"},{"content":"else"}] //etc...

So you are using ng-repeat for something and your ids are there. 因此,您正在使用ng-repeat做某事,并且您的ID在那里。 If you use $index as value for your id parameter when you remove at item from the array, your array numbering is not longer the same and the id is not what you expect to be. 如果在从数组中删除项目时使用$ index作为id参数的值,则数组编号将不再相同,并且id也不是您期望的值。

You also do not need the pop() and shift() methods. 您也不需要pop()和shift()方法。

Asuming your ng-repeat is something like this: 假设您的ng-repeat是这样的:

<element ng-repeat="item in someThing">
    {{item.prop}} <childelem ng-click=delete($index)>delete</childelem>
</element>

and if your function is: 并且如果您的职能是:

$scope.delete = function (id) {

    $localStorage.someArrayName.splice(id, 1);

};

Then you would need something like this: 然后,您将需要以下内容:

<element ng-repeat="item in someThing">
    {{item.prop}} <childelem ng-click=delete(item)>delete item</childelem>
</element>

and: 和:

$scope.delete = function (item) {

   $localStorage.someArrayName.splice($localStorage.someArrayName.indexOf(item), 1);

};

and this is going to remove the entire {"content":"something"} from your array. 这将从您的数组中删除整个{"content":"something"}

No idea if this is the right direction of the problem and if this is the scenario, but I hope it will help you. 不知道这是否是问题的正确方向,如果是这种情况,但我希望它将对您有所帮助。 Best regards! 最好的祝福!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM