0

I used $rootScope and $scope inside many controllers and services.I have reffered many stack overflow answer for clearing all $scope and $rootScope values solution.But it doesnt work for me(solutions like $rootScope=undefined or $rootScope=''). Kindly help me out of it.My Logout Code

  • Please insert code into the question as text, not as an image. You should also include any error messages as text, as well, because "it doesn't work" is not a good description of the problem. – Michael Gaskill Jun 16 '16 at 06:49

3 Answers3

4

You should delete your properties from $roorScope. It should be:

delete $rootScope.yourProperty1;
delete $rootScope.yourProperty2;

If have many properties, you should use:

for (var prop in $rootScope) {

   // Check is not $rootScope default properties, functions
   if (typeof $rootScope[prop] !== 'function' && prop.indexOf('$') == -1 && prop.indexOf('$$') == -1) {

      delete $rootScope[prop];

   }
} 
Huy Chau
  • 2,178
  • 19
  • 26
  • thanks for your answer. But i have 100+ properties. It works if i delete my individual property like you answered. Am asking if there is any other way for clearing all $rootScope and $Scope instead clearing individual property using delete – Varathan Swaminath Jun 10 '16 at 05:02
  • See my updated answer, I think you should delete your properties with for loop :) – Huy Chau Jun 10 '16 at 05:21
1

You can do the following

$rootScope.$on("logout", function(){
  $rootScope.userRole= undefined;});
zeeshan Qurban
  • 387
  • 1
  • 3
  • 15
1

If you wish to clear all the defined properties on $rootScope and want to retain the initial values that come with $rootScope, you can do this by deleting all the properties on $rootScope that do not start with $. As all the initial properties are defined with $.

$rootScope.$clearScope = function() {
    for (var prop in $rootScope) {
        if (prop.substring(0,1) !== '$') {
            delete $rootScope[prop];
        }
    }
}

Also, have a look to this post.

Community
  • 1
  • 1
Gopal Yadav
  • 368
  • 2
  • 8