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.
Asked
Active
Viewed 3,987 times
0

Varathan Swaminath
- 98
- 2
- 10
-
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 Answers
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
-
I have cleared the whole cache and tried dude.. It doesnt makes sense – Varathan Swaminath Jun 10 '16 at 05:24
-
ok try this code.$rootScope.$on("logout", function(){ $rootScope.userRole= undefined;}); – zeeshan Qurban Jun 10 '16 at 05:28
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