Javascript $state.go进入应用程序中的$stateChangeStart。运行不工作
注意:这是一个示例代码,Javascript $state.go进入应用程序中的$stateChangeStart。运行不工作,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,注意:这是一个示例代码,窗口已重定向只是为了使问题更简单 此代码不会重定向到仪表板 我在网上读到,它不起作用的原因是,$state还没有准备好。。然而,我在网上看到的教程与此非常相似 我怎样才能解决它 (function(){ "use strict"; angular.module('app.routes').config( function($stateProvider, $urlRouterProvider ) { $urlRouterProvider.otherwise('/
窗口已重定向
只是为了使问题更简单
此代码不会重定向到仪表板
我在网上读到,它不起作用的原因是,$state
还没有准备好。。然而,我在网上看到的教程与此非常相似
我怎样才能解决它
(function(){
"use strict";
angular.module('app.routes').config( function($stateProvider, $urlRouterProvider ) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('login',{
url: '/',
...
})
.state('dashboard', {
url: '/dashboard',
...
});
} ).run( function( $rootScope, Users, $state ) {
$rootScope.$on( "$stateChangeStart", function(event, toState, toParams
, fromState, fromParams){
if ( !window.has_redirected ){
window.has_redirected = true;
window.console.log('going to dashboard');
$state.go('dashboard');
}
});
} );
})();
有
你就快到了。。。我们在这里真正需要的是通过以下方式停止执行流:
event.preventDefault();
它将停止当前状态更改,并执行重定向($state.go()
):
查看一下哇,我真不敢相信我竟然忘记了这件事!非常感谢你的回答,如果这很有帮助的话!享受强大的用户界面路由器,先生;)谢谢@RadimKöhler:)
$rootScope.$on( "$stateChangeStart", function(event, toState, toParams
, fromState, fromParams){
// I would always check if we are not already on the way
// to the "redirection" target.
var isGoingToDashboard = toState.name === "dashboard";
if(isGoingToDashboard)
{
return;
}
// this remains
if ( !window.has_redirected ){
window.has_redirected = true;
console.log('going to dashboard');
// HERE is the essential breaker of the execution
event.preventDefault();
$state.go('dashboard');
}
});