themasmul themasmul - 3 months ago 16
Javascript Question

Angular Cordova push data gone after reload

im new with angular, and i want create apps that store cart data to sesssion using push,
but when i reloading the page, the data session was gone, not like in the php $_SESSION,

heres my code

cartObj.cart.push( { "cart_item_id": id , "cart_item_image": image , "cart_item_name": name , "cart_item_price": price , "cart_item_qty": qty } );


how to make the cart data is store in session,
i'l try using local storage but it not work either

localStorage.setItem('cart_item_name', name);


in my controller

$scope.name= localStorage.getItem('cart_item_name');


im new here, hope someone care to answer and explain slowly,

thanks

edit

this my controller

.controller('keranjangBelanjaCtrl', ['$scope', '$stateParams','sharedCartService','$ionicPopup','$state', // TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams,sharedCartService,$ionicPopup,$state) {

//onload event-- to set the values
$scope.$on('$stateChangeSuccess', function () {
$scope.name= localStorage.getItem("cart_item_name");



});


this my view

<p>{{ name }}</p>


solved with

.controller('keranjangBelanjaCtrl', ['$scope', '$stateParams','sharedCartService','$ionicPopup','$state', // TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams,sharedCartService,$ionicPopup,$state) {

$scope.name= localStorage.getItem("cart_item_name");





);

Answer

You don't need to store the whole object in localstorage just Id of your cart item and get your cart item later by that Id. Currently you are storing just name. Item will be stored in localstorage until someone not remove it.

localstorage.setItem("cartId", id)

And get

localstorage.getItem("id") || 'null'