I need to clear all the $scope
values while performing some operations.
我需要在执行某些操作时清除所有$ scope值。
For eg: If I Click a "Signout"
button to redirect to "signin"
page, then all the $scope or $rootScope values in the session should be cleared.
例如:如果我单击“注销”按钮以重定向到“登录”页面,则应清除会话中的所有$ scope或$ rootScope值。
How can I achieve this?
我怎样才能做到这一点?
2 个解决方案
#1
16
You can do the following:
您可以执行以下操作:
$rootScope = $rootScope.$new(true);
$scope = $scope.$new(true);
The function $new
is creating a new scope inheriting the variables from the parent. true
prevents the inheritance.
函数$ new正在创建一个继承父项变量的新范围。 true可防止继承。
But this is not the correct approach, because if you use the thing above, you should bootstrap the controllers functions manually and recreating the tree of scopes.
但这不是正确的方法,因为如果你使用上面的东西,你应该手动引导控制器功能并重新创建范围树。
This might be useful though, where the idea is to store the initialized data is stored in some variables and then, when assigned copied to the displayed variables.
这可能是有用的,其中的想法是存储初始化的数据存储在一些变量中,然后,当分配复制到显示的变量时。
The correct solution is to clear manually every property in each scope on the logout event like this: Logout event:
正确的解决方案是手动清除注销事件中每个范围内的每个属性,如下所示:注销事件:
$rootScope.$emit("logout");
Catching the event:
抓住活动:
$rootScope.$on("logout", function(){
$rootScope.myData = undefined;
});
Or as suggested in the comments, to use a service and then be cleaned.
或者按照评论中的建议,使用服务然后进行清理。
#2
0
You not want delete scope
var authScope =['authLogo','currentPath','pageTitle','app'];
for (var prop in $rootScope) {
if (prop.substring(0,1) !== '$') {
if(authScope.indexOf(prop) ==-1)
delete $rootScope[prop];
}
}
#1
16
You can do the following:
您可以执行以下操作:
$rootScope = $rootScope.$new(true);
$scope = $scope.$new(true);
The function $new
is creating a new scope inheriting the variables from the parent. true
prevents the inheritance.
函数$ new正在创建一个继承父项变量的新范围。 true可防止继承。
But this is not the correct approach, because if you use the thing above, you should bootstrap the controllers functions manually and recreating the tree of scopes.
但这不是正确的方法,因为如果你使用上面的东西,你应该手动引导控制器功能并重新创建范围树。
This might be useful though, where the idea is to store the initialized data is stored in some variables and then, when assigned copied to the displayed variables.
这可能是有用的,其中的想法是存储初始化的数据存储在一些变量中,然后,当分配复制到显示的变量时。
The correct solution is to clear manually every property in each scope on the logout event like this: Logout event:
正确的解决方案是手动清除注销事件中每个范围内的每个属性,如下所示:注销事件:
$rootScope.$emit("logout");
Catching the event:
抓住活动:
$rootScope.$on("logout", function(){
$rootScope.myData = undefined;
});
Or as suggested in the comments, to use a service and then be cleaned.
或者按照评论中的建议,使用服务然后进行清理。
#2
0
You not want delete scope
var authScope =['authLogo','currentPath','pageTitle','app'];
for (var prop in $rootScope) {
if (prop.substring(0,1) !== '$') {
if(authScope.indexOf(prop) ==-1)
delete $rootScope[prop];
}
}