Jad*_*ran 2 javascript angularjs angular-ui-router
注意:这是一个示例代码,window.has_redirected只是为了使问题更容易.
此代码不会重定向到dashboard.
我在网上看到它之所以$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');
}
});
} );
})();
Run Code Online (Sandbox Code Playgroud)
你几乎就在那里......我们真正需要的是通过以下方式停止执行流程:
event.preventDefault();
Run Code Online (Sandbox Code Playgroud)
这将停止当前的状态更改,并将执行redirect ($state.go()):
$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');
}
});
Run Code Online (Sandbox Code Playgroud)
在这里检查它
| 归档时间: |
|
| 查看次数: |
2884 次 |
| 最近记录: |