angularjs – $rootlcope在$urlRouterProvider.otherwise中未定
发布时间:2020-12-17 17:21:04 所属栏目:安全 来源:网络整理
导读:我在$urlRouterProvider.otherwise中使用它时,$rootScope是未定义的. 我的目标是检查用户当前是否登录,在登录期间我在rootScope上设置变量. 有趣的是,如果我让$rootScope成为第一个参数,那么我开始得到$injector的错误. $urlRouterProvider.otherwise(functi
我在$urlRouterProvider.otherwise中使用它时,$rootScope是未定义的.
我的目标是检查用户当前是否登录,在登录期间我在rootScope上设置变量. 有趣的是,如果我让$rootScope成为第一个参数,那么我开始得到$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" } }); 解决方法
来自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”); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |