简体   繁体   English

持久数据应保存在Angular应用中的何处

[英]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. 我正在构建一个需要访问数据集的应用程序,该数据集由引导该应用程序的页面获取并保存为该页面中javascript变量中的JSON对象...显然,我可以从该应用程序内访问该数据集。

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. 因此,我编写了一个带有一些getter / setter方法的服务,使我可以获取或更改其中存储的数据。

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: 并将其继续存储在父/引导页面的本机JavaScript中,在该页面中,我将执行以下操作来访问它:

$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)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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