如何重置$ rootScope? [英] How to reset $rootScope?

查看:204
本文介绍了如何重置$ rootScope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户注销后,我需要清理我的$ rootScope。我试图 $ rootScope。$摧毁(),但没有做的伎俩。有没有一种方法来遍历$ rootScope所有值并将其删除或方法简单地重置?

After user logs out, I need to clean up my $rootScope. I tried $rootScope.$destroy() but that didn't do the trick. Is there a way to loop through all the values in $rootScope and delete them or a method to simply reset it?

推荐答案

您可能希望保留附带 $ rootScope 初始化时的默认值。由于他们所有开始 $ 您可以删除不符合 $ 开头的所有属性。

You may wish to retain the default values that come with $rootScope when it is initialized. Since they all begin with a $ you can delete all the properties that don't start with $.

for (var prop in $rootScope) {
    if (prop.substring(0,1) !== '$') {
        delete $rootScope[prop];
    }
}

您可以很容易地调用可以将其作为一个功能上的 $ rootScope

You could make it easy to call by adding it as a function on $rootScope.

$rootScope.$resetScope = function() {
    ...
}

这篇关于如何重置$ rootScope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆