1

Hi guys i am trying to save some information with localStorage in angular, i injected $window to my service and i created a factory call $localStorage

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

i have other factory where i make us of the localStorage factory in order to save some favorites

factory("favoriteFactory", ["$resource", "baseURL", "$localStorage", function($resource, baseURL, $localStorage) { var favFac = {}; var favorites = $localStorage.getObject("favorites", "[]"); favFac.addToFavorites = function(index) { for (var i = 0; i < favorites.length; i++) { if (favorites[i].id == index) return; } $localStorage.storeObject("favorites", {id: index}); //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; }]) 

the problem is when i add a favorite item, it replaces itself in my array, instead of adding a new one to the array,

i really aprecciate the help thanks in advance

3 Answers 3

2

You are doing wrong while storing. You are replacing an array with a single item. One more thing to note that, Array.prototype.push() return the length of the collection.

enter code herefavFac.addToFavorites = function(index) { for (var i = 0; i < favorites.length; i++) { if (favorites[i].id == index) return; } favorites.push({id: index}) $localStorage.storeObject("favorites", favorites); //favorites.push({id: index}); }; 
Sign up to request clarification or add additional context in comments.

3 Comments

Array.prototype.push() does not return the item. it returns the length
@mJunaidSalaat Thank you Junaid, I actually tried to push 4 in an array [1,2,3] and it returned me 4, so I thought it returns item. Thanks for correction. but bad for negative vote :P
No problem. You can edit your answer and I'll remove the negative vote and at first you didn't answered the question correctly that's why I made an answer but you edited it later. :)
1

You just need to change addToFavorites method like

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

Now it will add an item first then save your array into the local storage.

Comments

0

As an advice I suggest you to use ngStorage which enables you to add or remove item from localStorage as simples as a single command:

$localStorage.favorites = []; 

That's it and now you have favorites list in localStorage and any time you modify this array you will get results directly on localStorage.

$localStorage.favorites.push(newItemToAdd); // this adds a item. $localStorage.favorites = $localStorage.favorites .filter((v, i) => i !== indexOfItemToDelete); // removes item. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.