Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 这是使用$rootscope将服务存储为变量的正确方法吗_Angularjs - Fatal编程技术网

Angularjs 这是使用$rootscope将服务存储为变量的正确方法吗

Angularjs 这是使用$rootscope将服务存储为变量的正确方法吗,angularjs,Angularjs,我有一个功能,可以在用户长时间没有向服务器发送请求后处理注销应用程序(下面的代码中是CommonConfig.time\u OUT\u to\u logout)。我使用$rootScope将logOutInterval存储为变量。但我想知道,如果我使用$rootScope作为正确的方法 handleSuccess是一个处理请求$http success的函数 对不起,我的英语不好 function handleSuccess(response) { $anchorSc

我有一个功能,可以在用户长时间没有向服务器发送请求后处理注销应用程序(下面的代码中是
CommonConfig.time\u OUT\u to\u logout
)。我使用$rootScope将
logOutInterval
存储为变量。但我想知道,如果我使用$rootScope作为正确的方法

  • handleSuccess
    是一个处理请求$http success的函数
对不起,我的英语不好

function handleSuccess(response) {
            $anchorScroll(); // scroll to top after load API completed
            if ($rootScope.logOutInterval) {
                console.log("Reupdate count down to logout");
                $timeout.cancel($rootScope.logOutInterval);
                $rootScope.logOutInterval = undefined;
            }
            console.log("Start count down to logout at " + new Date());
            $rootScope.logOutInterval = $timeout(function() {
                console.log("Start logout at " + new Date());
                $http({
                    method: 'get',
                    headers: defaultHeader(),
                    url: envConfig.API_HOST + CommonConfig.URI.LOG_OUT
                }).then(function() {
                    toastr.error("Token has expired");
                    PageService.logOutAction();
                });
            }, CommonConfig.TIME_OUT_TO_LOGOUT);
            return $q.resolve(response);
        }

如果您只想在控制器之间存储和共享数据,或者希望在控制器的生命周期之外存储和共享数据,那么最佳做法是使用服务


如果您只想在服务中存储数据,则无需使用$rootScope。

如果您只想在控制器之间存储和共享数据,或在控制器的生命周期之外存储和共享数据,最佳做法是使用服务


如果您只想在服务中存储数据,无需使用$rootScope。

我明白您的意思,但仍然不明白您面临的问题是什么?谢谢您的评论。我是AngularJS新手,所以我不知道如何正确使用$rootScope(就在这种情况下)。在上面的例子中,是否有错误或不好的做法?在不使用$rootScope的情况下,是否有其他方法(作为替代方法)可以做到这一点?再次感谢:)可能重复我知道你想说的,但仍然不知道你面临的问题是什么?谢谢你的评论。我是AngularJS新手,所以我不知道如何正确使用$rootScope(就在这种情况下)。在上面的例子中,是否有错误或不好的做法?在不使用$rootScope的情况下,是否有其他方法(作为替代方法)可以做到这一点?再次感谢:)可能的副本