Ionic local storage vs using service

2019-01-28 16:56发布

I am finding myself placing all my data from my api calls in the device local storage and I am not sure if it matters whether I put things in local storage vs putting them in a service. When should I use local storage from device vs using a Angularjs service?

3条回答
在下西门庆
2楼-- · 2019-01-28 17:35

You can use this localstorage plugin for your app. https://github.com/grevory/angular-local-storage

查看更多
该账号已被封号
3楼-- · 2019-01-28 17:40

Data stored in local storage is persistent. So, if you reload you web app the data in local storage is till there. Local storage is typically limited to 5MB. See this

Services are in memory constructs. So, if you place anything in service it is in the browser's memory and are lost when refreshing the browser.

So it depends on what you need.

查看更多
相关推荐>>
4楼-- · 2019-01-28 17:44

You are a bit mingled up with concepts. When it comes to angularjs services, they persist data as long as the page is not refreshed, as soon as you refresh your page or close the browser tab, The data's gone.

Consider Angularjs services as mere variables that you declare, which are scoped to the lifetime of your browser tab. Hence, you can use it to store some temporary flags and values that aren't meant to be carried forward to next session.

Whereas, When it comes to localStorage, consider it as a database kind of stuff. Whatever you store in localStorage, is saved inside the browser, and will be available across multiple tabs, and sessions of your apps [Until and unless user clears browser data].

Since you're using Ionic and Cordova, you must use localStorage to save stuff such as user name and password, so that the user can use them the next time he opens your app. Take a note that, closing your app is equivalent to closing a browser tab.

Whereas, if you have certain data that keeps refreshing each time user visits your app, you can use services to store them, so that they are removed as soon as the app's closed.


Metaphorically, localStorage --> Secondary, non-volatile Storage, angularjs services --> Primary, volatile storage.

查看更多
登录 后发表回答