javascript - How to clear $rootScope completely when my application gets signout - Stack Overflow

I used $rootScope and $scope inside many controllers and services.I have reffered many stack overflow a

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.

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.

Share Improve this question asked Jun 10, 2016 at 4:53 Varathan SwaminathVarathan Swaminath 982 silver badges11 bronze badges 1
  • 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 Commented Jun 16, 2016 at 6:49
Add a ment  | 

3 Answers 3

Reset to default 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];

   }
} 

You can do the following

$rootScope.$on("logout", function(){
  $rootScope.userRole= undefined;});

If you wish to clear all the defined properties on $rootScope and want to retain the initial values that e 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745435408a4627584.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信