i am getting $rootScope is undefined when using it from inside $urlRouterProvider.otherwise.
My objective is to checkout if user is currently loggedin or not, during login i set the variable on rootScope.
Interestingly if i make the $rootScope the first argument, then i start getting 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"
        }
    });
                From urlRouter source codes
* @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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With