2016-06-21 4 views
0
아래 그림처럼 내가 자동으로 문자열로 객체에서 변환 배열을 저장하려고하면, 로컬 스토리지를 사용하여 이온에 문제가있어

:이온 로컬 스토리지

enter image description here

수있는 사람 도와주세요? 사용중인 코드는 다음과 같습니다.

angular.module('mainApp') 
    .factory('cartFactory', function() { 
     var cart = [{ 
      'title': 'titre2', 
      'pic': './img/catalogue/k2.jpg' 
     }]; 
     console.log("Type 1:" , typeof(cart)); 
     console.log("Content 1:" , cart); 
     window.localStorage.setItem('cart', cart); 
     var cart = window.localStorage.getItem('cart'); 
     console.log("Type 2:" , typeof(cart)); 
     console.log("Content 2:" , cart); 

     return { 
      all: function() { 
       return cart; 
      }, 
      get: function (index) { 
       return cart[index]; 
      }, 
      add: function (product) { 
       cart.push(product); 
      }, 
      remove: function (product) { 
       var index = cart.indexOf(product); 
       cart.splice(index, 1); 
      } 
     }; 
    }); 

고마워요!

답변

2

로컬 스토리지는 당신이 가진 수있는 최선의 방법은 JSON 배열로 배열을 변환하는 것입니다, 문자열을 지원하고 그것을 다시 읽어 :

localStorage.setItem("cart", JSON.stringify(cart)); 

//... 
var cart = JSON.parse(localStorage.getItem("cart")); 
+0

감사합니다! 너무 간단했다. – fzwael