$ rootScope在$ urlRouterProvider.otherwise未定义 [英] $rootScope is undefined in $urlRouterProvider.otherwise

查看:307
本文介绍了$ rootScope在$ urlRouterProvider.otherwise未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从$ urlRouterProvider.otherwise内使用时

我得到$ rootScope是不确定的。

我的目标是,到结帐如果用户当前的loggedIn或不登录时我设置的变量上rootScope。

有趣的是,如果我让$ rootScope第一个参数,然后我开始越来越错误$注射器。

  $ urlRouterProvider.otherwise(函数($喷油器,$位置$ rootScope){
        的console.log(否则执行的);        尝试{            变量$状态= $ injector.get($状态);            toastr.options.positionClass =烤面包顶级全角;
            toastr [错误]($位置$$路径+,找不到页面,名称是区分大小写的!);
            toastr.options.positionClass =烤面包右上角            如果(typeof运算$ rootScope.currentUser ===未定义)
                $ state.go(登录);
            其他
                $ state.go(仪表板); //重定向到一个404错误页面
        }赶上(E){
          回归/登陆
        }    });


解决方案

urlRouter 源$ C ​​$ CS

  * @参数字符串{|}对象排除要重定向或功能的URL路径
*规则,返回的URL路径。功能版本传递两个参数:
*`$ injector`和`$ location`服务,并且必须返回一个URL字符串。
*
* @返回{}对象`$ urlRouterProvider` - `$ urlRouterProvider`实例
* /
this.otherwise =功能(规则){

使用变量$ rootScope = $ injector.get($ rootScope);

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

这篇关于$ rootScope在$ urlRouterProvider.otherwise未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆