restrict service from resetting after page reload

103 views
Skip to first unread message

Srikar Shastry

unread,
May 21, 2014, 9:56:52 AM5/21/14
to ang...@googlegroups.com
Hello,
I have two pages each supported by respective controllers. Now, I want to pass variable from first controller to another as later utilizes it as get query parameter (not the path parameter). I tried services but when user right clicks and clicks "open link in a new tab", the second controller receives am empty var. Same happens when user reloads the page. I tried local storage but the problem with that is when user tries to open multiple links, all point to the latest variable(true since local storage is being overwritten with latest vars as user clicks on links). Any idea on how to restrict services from reseting on page reload?

Eric Eslinger

unread,
May 21, 2014, 10:49:28 AM5/21/14
to ang...@googlegroups.com
As far as I know, the browser treats each window as a separate memory space (so shared objects are right out) and each reload/refresh event as a total restart. You could try being clever with localStorage (although rapid-fire clicks would probably lead to a race condition as you can't really guarantee instantiation order of the tabs, I imagine). 

It would also be helpful to store all relevant variables in your path, but if that's not available, you may need to look into using some kind of cross-window communication. I *think* (I've never actually tried this) that a tab opened via open-in-new-tab maintains a reference to the parent window in window.opener, so could get get clever with all kinds of message events.

Best bet is storing unreconstructable state in the URL, I think. That seems to me like what it's for.

e


On Wed, May 21, 2014 at 6:56 AM, Srikar Shastry <srikar...@gmail.com> wrote:
Hello,
I have two pages each supported by respective controllers. Now, I want to pass variable from first controller to another as later utilizes it as get query parameter (not the path parameter). I tried services but when user right clicks and clicks "open link in a new tab", the second controller receives am empty var. Same happens when user reloads the page. I tried  local storage but the problem with that is when user tries to open multiple links, all point to the latest variable(true since local storage is being overwritten with latest vars as user clicks on links). Any idea on how to restrict services from reseting on page reload?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Eric Eslinger

unread,
May 21, 2014, 10:54:58 AM5/21/14
to ang...@googlegroups.com
yeah, chrome at least sets window.opener on o-i-n-t windows to the parent tab. Just realized I could test this just in the devtools without mucking around with messageEvents - you just need to check if window.opener is null or not.

e

Srikar Shastry

unread,
May 21, 2014, 1:14:26 PM5/21/14
to ang...@googlegroups.com
I dont know if this is the "correct" way to do this but solves my problem.
I changed the URLs to have that variable using $routeParams. Now, in the later controller I say:

var loc = $location.url();
$scope.query_param_using_which_ajax_call_is_made = loc.split('/')[2];

and then I got ahead with ajax call.
Since, the url doesn't change with page refresh and new tab will always have that right url, my application is pointing to the correct ajax calls.

If any other sleek way to solve this problem is available, please let me know.

Thank you.
Reply all
Reply to author
Forward
0 new messages