0

I need to use a global info in my project.

I try to do like this:

app.service('userData',function(){

    var data;
   this.set = function(value) {
        return data = value;
    }

    this.data = function() {
        return data;
    }

});

But if I do refresh on the page, I lost it.

What's the best way to do it?

Thanks, Knot

6
  • Do you use ngRouter or ui-router? Commented Dec 3, 2015 at 21:40
  • Do you mean page refresh like F5 refresh? Commented Dec 3, 2015 at 21:42
  • Yeah.. F5 refresh the page... Commented Dec 3, 2015 at 21:45
  • Yes, I'm using ui-router.. Commented Dec 3, 2015 at 21:46
  • 1
    All JS variables that you create exists only within one page. So when you refresh the page your whole Angular application instance no longer exists and new one is created. Easiest way to persist such values would be to use localStorage. Commented Dec 3, 2015 at 21:48

1 Answer 1

1

You can use localStorage or cookies for keeping data.

Sign up to request clarification or add additional context in comments.

Comments

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.