使用$rootscope作为变量来存储服务是正确的方法吗?



我有一个函数来处理注销应用程序后,用户没有发送请求到服务器很长一段时间(它是CommonConfig.TIME_OUT_TO_LOGOUT在下面的代码)。我使用$rootScope来存储logOutInterval作为变量。但我想知道,如果我使用$rootScope作为正确的方式。

  • handleSuccess是处理request $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

最新更新