prash prash - 5 months ago 136
AngularJS Question

Clear $scope on logout in Angular js

In my controller I am storing data as

$scope.$parent.dossierSummaries = data;

but after log out and login the application
$scope.$parent.dossierSummaries
retains the same old data.

I am doing this on log out

.success( function( response, status ) {
if ( response.status > 0 ) {
var u = $rootScope.user.username;
$cookieStore.remove('myapp');
$rootScope.user = { username: '', role: 0 };
success(u);
}
else {
error(response.messages);
}
})
.error( function( response, status ) {
error(['There was an error logging you out.']);
});

Answer

I don't think there is any effective way to achieve it. Any object (controller, directive,filter or as a matter of fact any js object) can hold reference to another object (in your case user), and one cannot determine easily who all are holding reference.

The reference would only get release if you do it either explicitly or when the object holder the reference is destroyed.

What you can try is

$rootScope.user.username='';
$rootScope.role=0; 

Assuming some object are tracking this specific object the data would be cleared now.

Comments