1

I've got a question about storing data in AngularJS. Should I use some kind of service, or controller to keep data? I saw different codes in Angular and one time people store data in service, other, in controller. I would say that the proper way is to keep it inside factory, but is it a good practice?

Thanks!

5
  • why dont you just store data in json and fetch data using any services or factory . I think thats a simple solution Commented Nov 18, 2015 at 8:26
  • 1
    Your question is kinda hard to understand. Do you want to persist this data on the server? on the client? or it's just data that doesn't need to be stored at all (just for use by the view). Commented Nov 18, 2015 at 8:28
  • I mean data that isn't retrieving from a server, I rather talk about some local data like a list of coefficients which I'll use to make some calculations. Commented Nov 18, 2015 at 8:34
  • 2
    Data should be hold in services most of the time, controllers are the link between a service (and it's data) and the view. Keep the controllers slim, trim, and focused. Angular Style Guide by John Papa Commented Nov 18, 2015 at 8:37
  • 1
    And it doesnt matter if the service was createt with angular.service, angular.factory or angular.provider Commented Nov 18, 2015 at 8:38

2 Answers 2

2

Well, it depends on what you mean "storing data" for you...

If you mean keeping data in some place just for passing it between controllers, then this place is a service.

If you mean keeping data so it can persist even after a page refresh or after closing the browser and reopening the same app then you can use the javascript apis for sessionStorage and localStorage (take into account that older browsers may not have these apis and you would have to polyfill them). I've used an angular service for this that have given good results: https://github.com/tymondesigns/angular-locker.

If you mean persist the data for using it among other systems different than yours, the you should rely on a database server, either yours or from a third party (take a look at Firebird).

Of course you have more options, but they don't differ from the ones you have if you use plain javascript. Each of them could be treated in an "Angular way" if you create a service to manage them (IndexedDB, WebSQL, etc.) In the end it depends on what you're trying to achive.

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

Comments

0

If you need to use the data across multiple views / controllers, for example a logged in user, then i would recommend a service which holds the data for you. This way you can access it from anywhere you inject it. However, if you need your data only in one of your controllers, i dont see a reason to create a service, just store it in the controller.

1 Comment

Personally I'd still create a service even if it's just for one controller. That way you're keeping your controllers thin and logic consistent across the application.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.