rootScope not retaining value after hitting browser refresh

3,206 views
Skip to first unread message

Madhukar

unread,
May 14, 2014, 3:15:29 AM5/14/14
to ang...@googlegroups.com
Hello,

I'm a newbie in AngularJS faced with the following problem. 
I'm working a application which requires user to key in the branch detail while logging into the application.
This branch information needs to be used in subsequent controllers

So login.js which is a service has the following after successful authentication

        $rootScope.currentBranch = user.branch; // Works fine, gets printed on the console
        $location.path('/spend');

The spend path is wired to the template which is partial spend.html and which in turn is wired to the controller SpendCtrl
In SpendCtrl I'm using the alert to display the value of current branch

window.alert($rootScope.currentBranch);

Works perfectly well when it is re-directed to the spend.html for the first time and alerts the currentBranch the user used to login.

But when I hit F5 to refresh the browser, I'm alerted with an undefined instead of the currentBranch.
Can someone please help and explain to me what am I doing wrong here.

The angular version is 1.2.16 and the project has been generated using yeoman generator angular-fullstack.

In the same code, the $rootScope is assigned a resource object of user information details. Which I'm able to alert in the controller with the correct value evenr after hitting refresh multiple times.

Madhukar

unread,
May 14, 2014, 3:27:04 AM5/14/14
to ang...@googlegroups.com
Correctiion login.js is a Controller not a service as mentioned earlier

Sander Elias

unread,
May 14, 2014, 4:05:27 AM5/14/14
to ang...@googlegroups.com
Hi,

Hitting F5 is the equivalent of closing and opening your browser. Hence, you start fresh, with nothing in memory. 
There is nothing angular cab do about that. If you want to retain your users session you have to store it. 
A cookie or localStorage are mostly used for this kind of stuff. Do not store sensitive data this way.

Regards
Sander

Madhukar

unread,
May 14, 2014, 4:30:12 AM5/14/14
to ang...@googlegroups.com
Sander,

Thank you so much for your reply. 

The user information which is stored as $rootScope.currentUser=user is getting retrieved after hitting F5 multiple times.
It could be that its getting stored in the cookie as you mention.
I think I've tried using the cookieStore but ended up with $rootScope.currentBranch being undefined.
Let me try again and will keep you posted.

Best Regards
Madhukar

Madhukar

unread,
May 14, 2014, 8:39:39 AM5/14/14
to ang...@googlegroups.com
Sander,

Thank you so much, was able to use the cookieStore to persist the information.

The way I've persisted the branch details is by setting the $rootScope.branch= $cookieStore.get('branch') in the service.
This way, I'm able to now use the $rootScope.branch in the  controller which earlier was not possible after hitting F5 in the browser.

But in the service, if use the following code, I still end up getting undefined after hitting F5 on the browser
$rootScope.branch = $cookieStore.get('branch') || null;
$cookieStore.remove('branch'); //Works well if I comment this line of code not removing the branch info

which means I'm tied to using $cookieStore.
The question now is is this a right way or is there any better way of doing this.


Best Regards
Madhukar
Reply all
Reply to author
Forward
0 new messages