简体   繁体   中英

Where should persistent data be saved in an Angular app

I'm building an app that requires access to a data set that is fetched by the page that bootstraps the application and saved as a JSON object in a javascript variable in that page... which I obviously have access to from within the app.

The data is used throughout the app for various functionality. Additionally, some of that data is changed an/or new data is added.

Everything I've read states that persistent data like that should be saved in a service or factory, since they are singletons and they can be easily injected into any controller that needs access to it. So I wrote a service with some getter/setter methods that will allow me to grab or change data stored within it.

I'm just wondering what the difference is between storing the data in a service, where I'd do something like this to access it:

$scope.userAddr1 = PersistentData.get('profileData', 'userAddr1');

and continuing to store it in the native JavaScript of the parent/bootstrapping page where I'd do something like this to access it:

$scope.userAddr1 = profileData.userAddr1; 

I can see how storing it in the service would help keep things organized, but are there any other reasons why this is considered to be best practice?

如果您的客户端使用的是现代浏览器,则可以使用Web存储 (localStorage或sessionStorage)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM