Data transfer from one page to other


#1

Hi, I am new to ionic. I have a issue regarding transfer of data from one screen to other. I will explain my problem with an example.
I have two pages “A” and “B”.In both the screens I have a button called “Purchase”. Now I am navigating form page “A” to page “B”. In page “B” I am clicking on “Purchase” button upon clicking purchase button I am hiding the Purchase button and storing a BOOL variable in window.localStorage and I am clicking back button. Now I am in page “A”.When I am in page “APurchase button should be hidden depending upon the BOOL variable in window.localStorage . My problem is when I click back button in Page “B” and returning to Page “A”, which method should I call to check the Bool variable in local Storage. Please help me and let me know if you have any queries. Thanks in Advance


#2

LocalStorage is not the simplest approcach, you should rather define a Purchase service.

Anyway, your problem is probably also due to Ionic view caching, check the official docs or search the forum to know about it.


#3

@gmarziou is correct. You should not use localstorage for this feature when there are much better solutions.

As @gmarziou told you, create a Purchase service, it’s the best solution.

A dirty solution would be to use a global object. You can even share data between view transitions.

Use localstorage only if you want to persist some data.

If you need a tutorial, click here.


#4

Yeah got it, but out of curiosity in iOS by using protocols we can pass data when we are popping to the last visited view controller. Can we implement the same in ionic framework?? I mean to say while returning to the last visited page by clicking on back button, can we transfer any data to that page??


#5

It depends on how are you handling back button. Are you handling it manually or are your back buttons generated automatically?


#6

It is an auto generated back button by ionic @Gajotres


#7

Unfortunately, this is not possible, at least not in my knowledge.

Factory service is still your best choice.


#8

Yeah I got it by using ionic Config Provider and making the cached views to zero Will check with factory services. Thanks for your answers @Gajotres and @gmarziou :smiley: