0

I have a function to handle logout application after user did not send request to server for a long time (it's CommonConfig.TIME_OUT_TO_LOGOUT in the code below). I use $rootScope to store logOutInterval as a variable. But I wonder that, if I used $rootScope as a right way.

  • The handleSuccess is a function that handle request $http success.

Sorry for my bad English.

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);
        }
  • I got what you are trying to say, but still not got that what is the issue that you are facing? – Jigar7521 Oct 17 '16 at 12:03
  • Thank you for your comment. I'm a AngularJS newbie , so I don't know how to use $rootScope as a right way (just in this situation). In the example above, is there any wrong or bad practice? is there any other way (as an alternative) to do this without using $rootScope? Thank you again :) – Nguyễn Bá Vinh Oct 17 '16 at 15:36
  • Possible duplicate of [Storing data in $rootScope](http://stackoverflow.com/questions/38651880/storing-data-in-rootscope) – georgeawg Oct 17 '16 at 16:49

1 Answers1

2

If you just want to store and share data between controllers, or something that will outlive the controller's lifecycle, the best practice is use services for that.

If you just want to store data inside a service, no need to use $rootScope for that.

Felipe
  • 474
  • 1
  • 3
  • 10