Clearing $rootScope

1,867 views
Skip to first unread message

tteh....@gmail.com

unread,
Nov 8, 2012, 6:26:18 PM11/8/12
to ang...@googlegroups.com
Hi there, 

I am just wondering if there is any way to reset the $rootScope? I'm running into the problem where I have an authentication process and after I've logged out, I'm back on the login page. However, when there is a failed login, $digest seems to check the views that are beyond the authentication process for changes which breaks my application. Is there an example on how to deal with this? 

As a note, I'm using jQuery for my ajax requests and in my callbacks, I use $apply to update my views.

Thanks

Peter Bacon Darwin

unread,
Nov 9, 2012, 2:26:43 AM11/9/12
to ang...@googlegroups.com
If you have attached something to the rootScope yourself you can simply delete it from the scope.  If there are child scopes with stuff attached you can have the controller who attached it remove it.  You may consider putting shared data into a service so that you have control over it?

Perhaps you could create a fiddle or a plunker to demonstrate the problem?

Pete

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

tteh....@gmail.com

unread,
Nov 9, 2012, 9:25:50 AM11/9/12
to ang...@googlegroups.com
Thanks Pete. It's automatically attached and I'm not even clear on what the problem is but after setting breakpoints, I've noticed that the issue happens when the watchers for the authenticated views get called because after I supposedly log out. 

Here's my entire flow:
On the login page, I make an Ajax request and it authenticates with the server and switches the view to the admin view. On the admin view, I logout and clear my variables. Now that I'm back on the login page, I attempt to login with an invalid password - this page gets refreshed. It's driving me nuts because I'm not sure why. It seems to get refreshed while digesting and only after digesting the templates in the admin view.

I'll try to work out a fiddle but I'm not sure if I can actually reproduce it. I'm sorry if it's confusing - it might warrant a rewrite anyway because my click handlers don't seem to be fired for AngularJS testing.

Thanks

Peter Bacon Darwin

unread,
Nov 9, 2012, 9:37:07 AM11/9/12
to ang...@googlegroups.com
Try to create an authentication service that maintains info about whether user is logged in.  Then make all the rest of your application work from this and not copy that information.  Each view could check this service before it is shown.

Ami Kapadia

unread,
Dec 11, 2014, 8:47:40 AM12/11/14
to ang...@googlegroups.com, tteh....@gmail.com
Hi! Have you found any solution to reset $rootScope?

Ward Bell

unread,
Dec 13, 2014, 1:30:02 PM12/13/14
to ang...@googlegroups.com, tteh....@gmail.com
The solution is "don't do that". 

IMO,  if you find yourself needing to clear the $rootScope, you've made a fundamental architectural error. Are you adding stuff to the $rootScope and treating it like a global. Doing that is no different than polluting the window with global variables ... and it's "bad" for the same reasons. If you're need to guard views against unauthorized access, follow Pete's advice and have each view guard itself. If you're using routing to navigate views, you can make the router prevent unauthorized navigations.
Reply all
Reply to author
Forward
0 new messages