javascript ui-router - $state.go() 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31449948/
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
ui-router - $state.go() not working
提问by Ruben Renzema
Here is the app.js of my project:
这是我的项目的 app.js:
(function () {
'use strict';
angular
.module('app', ['ui.router', 'ngCookies', 'angular-inview', 'ngMaterial'])
.config(config)
.run(run);
config.$inject = ['$stateProvider', '$urlRouterProvider', '$mdThemingProvider'];
function config($stateProvider, $urlRouterProvider, $mdThemingProvider) {
$mdThemingProvider.theme('default')
.primaryPalette('deep-orange')
.accentPalette('teal', {
'default': 'A400'
});
$urlRouterProvider.otherwise('/app');
$stateProvider
.state('app', {
url: '/app',
data: {
clearenceLevel: 1
},
views: {
'': {
templateUrl: 'app/views/navbar.html',
}
}
})
.state('home.works', {
url: '/works',
templateUrl: 'app/views/works.html',
controller: 'WorksController as vm'
})
.state('login', {
url: '/login',
templateUrl: 'app/views/login.html',
controller: 'LoginController as vm',
data: {
clearenceLevel: 0
}
})
.state('register', {
url: '/register',
templateUrl: 'app/views/register.html',
controller: 'RegisterController as vm',
data: {
clearenceLevel: 0
}
});
}
run.$inject = ['$rootScope', '$location', '$state', '$cookieStore', '$http', 'AuthenticationService'];
function run($rootScope, $location, $state, $cookieStore, $http, AuthenticationService) {
$rootScope.globals = $cookieStore.get('globals') || {};
if ($rootScope.globals.currentUser) {
$http.defaults.headers.common['aimm-token'] = $rootScope.globals.currentUser.token;
$http.defaults.headers.common['aimm-id'] = $rootScope.globals.currentUser.id;
}
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
var clearenceLevel = toState.data.clearenceLevel;
var loggedIn = $rootScope.globals.currentUser;
if (clearenceLevel > 0 && !loggedIn) {
alert("redirecting");
return $state.go('login');
}
});
}
})();
I just can't have $state.go() working. The $on('$stateChangeStart'...);
is working fine, and the alert is poping when trying to reach a protected state with no session. But the return $state.go('login');
doesnt work. It redirects to /app
.
我只是不能让 $state.go() 工作。该$on('$stateChangeStart'...);
工作正常,并试图用没有会话达到保护状态时,警报坡平。但return $state.go('login');
不起作用。它重定向到/app
.
Thanks for your help.
谢谢你的帮助。
回答by Ruben Renzema
Well, thanks to @Vanojx1, I found out adding e.preventDefault();
before the $state.go('login');
made it work. Still dont understand why though.
好吧,感谢@Vanojx1,我在使它工作e.preventDefault();
之前发现了添加$state.go('login');
。还是不明白为什么。
回答by ahmadalibaloch
You need to execute the $state.go
on main scope and not in a (angular) service or virtual scope created temporarily.
您需要$state.go
在主作用域上执行,而不是在临时创建的(角度)服务或虚拟作用域中执行。
You can also solve the problem by wrapping it in $timeout
or setTimeout
which will register it in the browser loop to be executed after the current method run etc even with 0 milliseconds will do it, like.
您还可以通过将其包装$timeout
或setTimeout
将其注册到浏览器循环中以在当前方法运行后执行等来解决问题,即使使用 0 毫秒也会这样做,例如。
$timeout(()=>{$state.go('xyz')},0)
or
或者
setTimeout(()=>{$state.go('xyz')},0);