Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$rootScope is undefined in $urlRouterProvider.otherwise

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"
        }

    });
like image 552
NSS Avatar asked Mar 15 '23 15:03

NSS


1 Answers

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");

like image 56
sbedulin Avatar answered Mar 21 '23 08:03

sbedulin