Passing Data Between Pages in AngularJS + Page Refresh

后端 未结 2 1578
庸人自扰
庸人自扰 2020-12-28 22:00

I am trying to pass data between pages in the checkout process of an app but it\'s not working the way it should. I have done some reading and most people recommend using a

2条回答
  •  孤城傲影
    2020-12-28 22:31

    There are several options to do this,

    1. For smaller data sets you could use the $cookieStore, for data that is under 4k
    2. Another option, especially with large data sets, would be to use Local Storage and then retrieve the data on page load/reload.
    3. if it is only a very small amount of data, or data that is used through out multiple page you could use $rootscope, but this is not the best option as it just like polluting the global name space.
    4. The last option, depending on how the data is retrieved, a service could be implemented, that is basically a singleton that can be passed to various angular scope.

    Note: only the first two are persistent.

    In your case I think that using local storage or the cookiestore will be you best options. You are trying to use a service, which would be appropriate if you did not want it to be persistent (leaving the page or a page refresh). Services are singletons that being managed by angular, when injected you will get a reference to the same object in each injection. However, when returning to the page this singleton will need to be re initialized, thus losing all previous data. The only way to make make a service persistent would be to load the data from a database, a local file, or noSQL from elsewhere. However, I do not think this is really what you are after.

    If you are interested in pursuing the local storage implementation then look into these modules angular-local-storage, ngStorage or this answer

    If you want to use the cookiestore look into this answer

提交回复
热议问题