I might have some kind of missunderstanding. Im using angular ui router and i have the next issue:
I have the next State provider:
angular
.module(\'Main
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
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.
if(isLogin("userId")){
$location.path("/main");
}else{
$location.path("/login");
}
Hope this help you solve your problem.
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
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 )
.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 ...
So i've made it work the different way, ive used $stateChangeStart and now my run function looks like this:
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...
I experienced a similar problem. $state.go not working in the .run function.
Placing $state.go in a service solved my problem.
Yep, as suggested it looks like $state.go() doesn't work correctly in module.run().
I found that putting the $state code in a $timeout works, e.g.
angular.module('MainApp').run($timeout, $state, ...) {
$timeout(function() {
$state.go('main');
});
}