$ rootScope - undefined in $ urlRouterProvider.otherwise

I get $ rootScope undefined when using it from inside $ urlRouterProvider.otherwise.

My goal is to check if the user is currently running or not, during login I set the variable to rootScope.

Interestingly, if I make $ rootScope the first argument, then I will start to get an error for $ injector.

$urlRouterProvider.otherwise(function ($injector, $location, $rootScope) {
        console.log("Otherwise Executed");

        try {

            var $state = $injector.get("$state");

            toastr.options.positionClass = "toast-top-full-width";
            toastr["error"]($location.$$path + ", Page not Found, names are case sensitive !");
            toastr.options.positionClass = "toast-top-right";

            if (typeof $rootScope.currentUser === 'undefined')
                $state.go("Login");
            else
                $state.go("dashboard"); //redirect to a 404 page
        } catch (e) {
          return "/login"
        }

    });
+4
source share
1 answer

From urlRoutersource code

* @param {string|object} rule The url path you want to redirect to or a function 
* rule that returns the url path. The function version is passed two params: 
* `$injector` and `$location` services, and must return a url string.
*
* @return {object} `$urlRouterProvider` - `$urlRouterProvider` instance
*/
this.otherwise = function (rule) {

Use var $rootScope = $injector.get("$rootScope");

+7
source

All Articles