javascript 在 Angular js 中注销时清除 $scope
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18509737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear $scope on logout in Angular js
提问by Prashobh
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.
在我的控制器中,我正在存储数据,$scope.$parent.dossierSummaries = data;
但是在注销并登录后,应用程序$scope.$parent.dossierSummaries
保留了相同的旧数据。
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.']);
});
采纳答案by Chandermani
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.
我认为没有任何有效的方法可以实现它。任何对象(控制器、指令、过滤器或事实上任何 js 对象)都可以持有对另一个对象(在您的情况下是用户)的引用,并且无法轻易确定谁都持有引用。
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.
假设某个对象正在跟踪该特定对象,则现在将清除数据。
回答by Dzung Nguyen
in angularJS, you shouldn't set the variable directly to a controller but you should retrieve it from a service instead. So whenever you load a controller you should write a init()
function to get value of that model. So everytime you will have the correct data from server.
在 angularJS 中,您不应将变量直接设置为控制器,而应从服务中检索它。因此,无论何时加载控制器,您都应该编写一个init()
函数来获取该模型的值。所以每次你都会从服务器获得正确的数据。
Code example and docs : http://docs.angularjs.org/guide/dev_guide.services.creating_services
代码示例和文档:http: //docs.angularjs.org/guide/dev_guide.services.creating_services
回答by checketts
Another approach to manually tracking and cleaning things up would be to broadcast
a 'logout' event on the rootScope
(or other custom event). Then listen for the event either in your controller or in your service to clean up the data.
手动跟踪和清理事物的另一种方法是针对(或其他自定义事件)broadcast
的“注销”事件rootScope
。然后在控制器或服务中监听事件以清理数据。
Broadcast:
播送:
$rootScope.broadcast('logout');
Watching for an event (in a service for example):
监视事件(例如在服务中):
$rootScope.on('logout',function(){
dossiers = [];
});
回答by Hamid Tavakoli
if you don't mind a slight screen flickering on logout, you can refresh the page using a method like this:
如果您不介意注销时轻微的屏幕闪烁,您可以使用如下方法刷新页面:
$window.location.replace($window.location.toString().split('#')[0]);
this would clear out all the $scope
and $rootScope
variables, and in my case has solved the issue.
这将清除所有$scope
和$rootScope
变量,在我的情况下已经解决了这个问题。
回答by tomazahlin
If you want to clear the $scope, I think you can use it's constructor method or proto(prototype), which is used in constructor. I believe you can do that to reset the scope to initial state. If someone knows any more on this, feel free to comment.
如果要清除$scope,我认为您可以使用它的构造函数方法或proto(原型),它在构造函数中使用。我相信您可以这样做以将范围重置为初始状态。如果有人对此有更多了解,请随时发表评论。