javascript module.run() 和 $state.go() angularJS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26355213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
module.run() and $state.go() angularJS
提问by dulebov.artem
I might have some kind of missunderstanding. Im using angular ui router and i have the next issue:
我可能有什么误解。我正在使用 angular ui 路由器,但我遇到了下一个问题:
I have the next State provider:
我有下一个 State 提供者:
angular
.module('MainApp')
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider ) {
$stateProvider
.state('register', {
url: "/",
abstact: true,
views: {
"side-bar": {
controller: "GetSideBarCtrl"
},
"page-body@register": {
templateUrl: "/App/Views/WelcomePage.html"
}
},
})
.state('register.Register', {
url: "Register",
views: {
"page-body": {
controller: "RegisterCtrl",
templateUrl: "/App/Views/Register.html"
}
},
})
.state('register.Login', {
url: "Login",
views: {
"page-body": {
controller: "LoginCtrl",
templateUrl: "/App/Views/Login.html"
}
},
})
//For registered and loged in users!
/* */
.state('main', {
url: "/:userId",
// abstact: true,
views: {
"side-bar": {
controller: "GetSideBarCtrl"
},
"page-body@main": {
// controller: "LoginCtrl",
templateUrl: "/App/Views/MainPage.html"
}
},
});
$urlRouterProvider.otherwise('/');
}]);
And run function
和运行功能
angular
.module('MainApp')
.run(['$rootScope', '$state', '$cookieStore', 'principal',
function ($rootScope, $state, $cookieStore, principal) {
var cookies = $cookieStore.get('user');
if (cookies) {
principal.Authenticate(cookies.split(' ')[0], cookies.split(' ')[1]);
$state.go('main', { userId: cookies.split(' ')[0] });
} else {
$state.go('register.Login');
}
}]);
Everything works fine, when cookies are present and authenication works without any problems but $state.go does nothing, i cant figure out why, can you please help and explain me...
一切正常,当存在 cookie 并且身份验证没有任何问题时,$state.go 什么也没做,我不知道为什么,你能帮我解释一下吗...
回答by Dunc
Yep, as suggested it looks like $state.go() doesn't work correctly in module.run().
是的,正如建议的那样,$state.go() 在 module.run() 中无法正常工作。
I found that putting the $state code in a $timeout works, e.g.
我发现将 $state 代码放在 $timeout 中是有效的,例如
angular.module('MainApp').run($timeout, $state, ...) {
$timeout(function() {
$state.go('main');
});
}
回答by dulebov.artem
So i've made it work the different way, ive used $stateChangeStart and now my run function looks like this:
所以我让它以不同的方式工作,我使用了 $stateChangeStart 现在我的运行函数看起来像这样:
angular
.module('MainApp')
.run(['$rootScope', '$state', '$cookieStore', 'principal',
function ($rootScope, $state, $cookieStore, principal) {
var cookies = $cookieStore.get('user');
if (cookies) {
principal.Authenticate(cookies.split(' ')[0], cookies.split(' ')[1]);
}
$rootScope.$on('$stateChangeStart',
function (event, toState) {
if ((toState.name === "register") && (principal.isAuthenticated)) {
event.preventDefault();
$state.go('main', { userId: principal.identity().userId });
}
});
}
]);
But i still dont know why $state.go() didnt work...
但我仍然不知道为什么 $state.go() 不起作用......
回答by Sean Out
I am having exactly the same problem.
I am using ionic to build a cross-platform mobile app.
I have logic like --> if the user is already logged in. redirect to the main page, otherwise redirect to login page
我遇到了完全相同的问题。
我正在使用 ionic 来构建一个跨平台的移动应用程序。
我有这样的逻辑 --> 如果用户已经登录。重定向到主页,否则重定向到登录页面
if(isLogin("userId")){
$state.go("main");
}else{
$state.go("login");
}
This work perfectly on Android but crash occasionally on IOS. After some investigation and research. I use $location.path instead of $state.go and everything works fine.
这在 Android 上运行良好,但在 IOS 上偶尔会崩溃。经过一番调查研究。我使用 $location.path 而不是 $state.go 一切正常。
if(isLogin("userId")){
$location.path("/main");
}else{
$location.path("/login");
}
Hope this help you solve your problem.
希望这可以帮助您解决问题。
回答by Mike Huebner
$state.go does not work in the run, it isn't initialized yet so! What I did was throw the logic in a service, add a controller to the module you have the run in, run it in the controller! That should work.
$state.go 在运行中不起作用,它尚未初始化,所以!我所做的是在服务中抛出逻辑,将控制器添加到您运行的模块中,在控制器中运行它!那应该有效。
回答by troig
Just point that following the advises of this thread, you can also solve it changing the otherwise block for this:
只需指出遵循此线程的建议,您还可以解决它更改其他块:
$urlRouterProvider.otherwise(function ($injector, $location) {
var $state = $injector.get("$state");
$state.go("/");
});
Hope it helps
希望能帮助到你
回答by Tyrone
I experienced a similar problem. $state.go not working in the .run function.
我遇到了类似的问题。$state.go 在 .run 函数中不起作用。
Placing $state.go in a service solved my problem.
将 $state.go 放在服务中解决了我的问题。
回答by amdev
It can sound strange but for me the solution was to rename my state and remove the dot ...
这听起来很奇怪,但对我来说,解决方案是重命名我的状态并删除点...
I want an home page different for customer, director, manager or anything ( but with the same url )
我想要一个针对客户、主管、经理或任何东西的不同主页(但具有相同的 url)
.state('home', {
url : '/',
templateUrl: 'views/home.html',
controller: 'HomeCtrl',
controllerAs: 'home',
module :'private'
})
.state('home.customer', {
url : '',
templateUrl: 'views/home-customer.html',
controller: 'HomeCustomerCtrl',
controllerAs: 'homeCustomer',
module :'private'
})
.run(function ($rootScope, $state, $window,userService) {
$rootScope.$on("$stateChangeStart", function(e, toState, toParams, fromState, fromParams) {
if(toState.name === 'home') {
if(userService.isCustomer()) {
$state.go('home.customer');
}
}
}
This dosen't work, replace the name home.customer
state with home-customer
and $state.go('home-customer');
and it works ...
这不起作用,home.customer
用home-customer
和替换名称状态$state.go('home-customer');
并且它起作用......