2016-02-21 61 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 –