gpt4 book ai didi

angularjs - $urlRouterProvider 中的 $rootScope 未定义。否则

转载 作者:行者123 更新时间:2023-12-04 23:44:52 25 4
gpt4 key购买 nike

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

关于angularjs - $urlRouterProvider 中的 $rootScope 未定义。否则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31005644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com