myApp.controller('editCtrl', function ($scope, adFactory) { $scope.adList = adFactory.ads; $scope.insertNewAd = function(){ adFactory.insertAd($scope.myAd); }; console.log($scope.adList) }); Help with problem solving. It is impossible to add an object to an array without changing the old ones. The object is added as if, but at the same time all old saved objects are overwritten.
myApp.service('adFactory', function() { var self = this; this.ads = []; this.insertAd = function(poster){ function getAds(){ self.ads = JSON.parse(localStorage.getItem('adstorage')); }; self.ads.push({ id: _.uniqueId(), title: poster, description: poster }); localStorage.setItem ('adstorage', JSON.stringify(self.ads)); }; });
self.ads = JSON.parse(localStorage.getItem('adstorage'));After this line to make output to the content consoleadsarray contains old data? - Bald