2015-06-23 36 views
4

我得到$ rootScope在$ urlRouterProvider.otherwise中使用时未定义。

我的目标是签出用户当前是否登录,在登录期间我在rootScope上设置变量。

有趣的是,如果我使$ rootScope的第一个参数,然后我开始获得$注入器的错误。

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

    }); 

回答

7

urlRouter源代码

* @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) { 

使用var $rootScope = $injector.get("$rootScope");