2016-02-21 24 views
0

下面是代码离子本地存储将信息放入数组

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); 
      } 
      } 
     }]) 

我想打一个收藏夹功能,我想把每个项目的ID标记到一个数组中。

但是,它不能扩大数组,只能改变数值。

我在这里弄错了吗?或者,也许我在这里放错了方法?

预先感谢您!

+0

你想存储喜欢的项目与本地存储阵列像第一我添加手机作为收藏,然后笔记本电脑,所以这两个值将在收藏夹阵列权? –

+0

是的,所以我希望我可以存储到这样的东西 {“id”:0},{“id”:7},{“id”:11} – Lyriene

回答

1

我只是创建用于存储对象的逻辑,您必须创建从localstorage中删除对象的逻辑。

<!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="[email protected]" 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> 
+0

我尝试实现你的方法,它运作良好!你救了我,非常感谢你! 要尝试创建一个逻辑来从本地存储中删除它 – Lyriene

+0

如果答案有帮助,那么请upvote –