0

I'm attempting to create a persistent Array throughout a client's session without actually using $window.sessionStorage. Right now every single time I change route the array empties out, even if it's the same exact route I was just on. Is it possible to make data persistent without using sessions or localStorage?

var a = [];

Pushing anything into it:

a.push(b);

Result of a after rerouting: [];

2 Answers 2

2

I would suggest using a service. A service in AngularJS is a singleton - meaning, that the same instance can be injected throughout the app.

This is better than the alternative of using the $rootScope, since it "pollutes" the scope and also does not lend itself to ease of testing with mocked injectables. It's hardly any better than using a global variable.

You could just create an injectable value that contains that array:

app.value("AVal", []);

and that would be enough. Of course, if you created a service, it would allow you to abstract away the details of the data structure:

app.factory("ASvc", function(){
  var a = [];

  return {
    add: function(val){
       a.push({v: val})
    },
    pop: function(){
      var item = a.splice(a.length - 1, 1);
      return item[0].v || null;
    }
  };
});

However you choose to do this, both are available as injectables, for example:

app.controller("MainCtrl", function($scope, AVal, ASvc){
   AVal.push({v: 5});

   // or
   ASvc.add(5);
});
Sign up to request clarification or add additional context in comments.

8 Comments

This is, as I posted, what I was trying to use. The array nonetheless empties upon route change. At this point I'm still using a factory but trying to implement a rootScope array into it, so it's (hopefully) available anywhere. Edit: Perhaps it's not working for me your way because I'm doing the pushing from inside the factory itself instead of a controller? That shouldn't make a difference, unless Angular just wants to be weird.
@mitbanip, in neither of the cases I showed the array would automatically be reset on route change. There is no need to use root scope here. At all. The rootScope-approach is suboptimal in every possible way.
I didn't know about .value()
No. These methods don't work for me. The array is indeed reset. I was already trying to do exactly what you're saying.
@mitbanip, then something else you are doing (that is not in your question) is resetting it. plnkr.co/edit/oAVYOOQxJKU2RhBHIqQa?p=preview
|
-1

Your controller function will re-run on route changes, clearing your local variables every time. There are a few ways to skin the cat here, but for something like this I would suggest using $rootScope, which is a special top level controller that won't re-run unless the whole app refreshes.

// controller
function WhateverController ($scope, $rootScope) {  
  // create array if one doesn't exist yet
  $rootScope.persistentArray = $rootScope.persistentArray || []
  $rootScope.persistentArray.push('Heyoo')

  $scope.localArray = $rootScope.persistentArray   
}

$rootScope can be passed to factories as well (pretty sure), but you can also achieve what you want with a typical factory, with properly scoped variables with getter / setters

3 Comments

I will give this a shot. Thank you.(Will mark as answer as soon as it allows me)
This doesn't work. I get a syntax error "." on this line. var $rootScope.cart= $rootScope.cart || [];
You shouldn't be putting var before $rootScope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.