离子本地存储将信息放入数组

Ionic Local storage put information into array

这是代码

Controller.js

$scope.addFavorite = function (index) {
            $scope.temp = {
                id: index
            };

            $scope.dish = $localStorage.getObject('favorites', '{}');
            console.log($localStorage.get('favorites'));

            $localStorage.storeObject('favorites', JSON.stringify($scope.temp));

            var favorites = $localStorage.getObject('favorites');
            console.log(favorites);

            favoriteFactory.addToFavorites(index);
            $ionicListDelegate.closeOptionButtons();
        }

Service.js

.factory('favoriteFactory', ['$resource', 'baseURL', function ($resource, baseURL) {
            var favFac = {};
            var favorites = [];

            favFac.addToFavorites = function (index) {
                for (var i = 0; i < favorites.length; i++) {
                    if (favorites[i].id == index)
                        return;
                }
                favorites.push({id: index});
            };

            favFac.deleteFromFavorites = function (index) {
                for (var i = 0; i < favorites.length; i++) {
                    if (favorites[i].id == index) {
                        favorites.splice(i, 1);
                    }
                }
            }

            favFac.getFavorites = function () {
                return favorites;
            };

            return favFac;
            }])    

.factory('$localStorage', ['$window', function($window) {
            return {
            store: function(key, value) {
              $window.localStorage[key] = value;
            },
            get: function(key, defaultValue) {
              return $window.localStorage[key] || defaultValue;
            },
            storeObject: function(key, value) {
              $window.localStorage[key] = JSON.stringify(value);
            },
            getObject: function(key,defaultValue) {
              return JSON.parse($window.localStorage[key] || defaultValue);
            }
          }
        }])

我想做一个收藏夹功能,我想把每一个标记的item的ID放到一个数组中。

但是,它无法扩展数组,只能更改值。

我在这里做错了吗?还是我放错方法了?

提前致谢!

我只是创建存储对象的逻辑,你必须创建从本地存储中删除对象的逻辑。

<!DOCTYPE html>
<html ng-app="plunker">

<head>
    <meta charset="utf-8" />
    <title>AngularJS</title>

    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script>document.write('<base href="' + document.location + '" />');</script>
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>

</head>
    <body ng-controller="MainCtrl">
        <div ng-repeat="item in items">
            {{item.item_name}} &nbsp; 
            <button ng-click="addFavorite(item.id)">Add to Favorite</button>
            <br><hr>
        </div>
    </body>
</html>

<script type="text/javascript">
    var app = angular.module('plunker', []);
    app.controller('MainCtrl', function($scope,$http) 
    {   
        $scope.items = [
            {id:1,item_name:'Apple'},
            {id:2,item_name:'Banana'},
            {id:3,item_name:'Grapes'},
        ]

        $scope.addFavorite = function (index) 
        {
            if(localStorage.getItem('favorites')!=undefined)
            {
                var old_favorite = JSON.parse(localStorage.getItem('favorites'));
                var obj = {index:index};
                old_favorite.push(obj);
                localStorage.setItem('favorites',JSON.stringify(old_favorite));
            }
            else
            {
                var obj = [{index:index}];
                localStorage.setItem('favorites',JSON.stringify(obj));
            }
        }
    });
</script>